Behavior of val() for multiple select in jQuery changed?

后端 未结 2 1729
野趣味
野趣味 2021-01-16 08:43

So everywhere I looked everybody says that to output values of selected options in a multiple select element one should use something like the following $(\'#selid :se

相关标签:
2条回答
  • 2021-01-16 09:07

    The val function gets the value of the first element in the set of matched elements. To get the value of multiple selected items, when using the :selected selector, you need to loop through the set, as you mention in your question.

    For more information, read the jQuery API for val().

    0 讨论(0)
  • 2021-01-16 09:08

    "in such case val() will return an array of values. Well, I can't replicate this behaviour."

    You never need to use :selected to get the value of a <select>. This is sufficient:

    $('#selid').val()
    

    Demo: http://jsfiddle.net/mattball/WZRmD/

    0 讨论(0)
提交回复
热议问题