JQuery - Get select value

后端 未结 2 457
悲哀的现实
悲哀的现实 2020-12-13 03:26

I\'m trying to get the selected value from a drop down list via jQuery. I have a bit of javascript that validates a form when I click SEND, to make sure there are no blanks,

相关标签:
2条回答
  • 2020-12-13 03:55

    val() returns the value of the <select> element, i.e. the value attribute of the selected <option> element.

    Since you actually want the inner text of the selected <option> element, you should match that element and use text() instead:

    var nationality = $("#dancerCountry option:selected").text();
    
    0 讨论(0)
  • 2020-12-13 04:03

    var nationality = $("#dancerCountry").val(); should work. Are you sure that the element selector is working properly? Perhaps you should try:

    var nationality = $('select[name="dancerCountry"]').val();
    
    0 讨论(0)
提交回复
热议问题