Getting the “newest” selected option's text from multiple select list

后端 未结 3 1446
[愿得一人]
[愿得一人] 2020-12-21 05:25

I have a HTML select list, which can have multiple selects:

 it returns an array of the values of its selected  elements.  We're simply storing that, and when the selection changes, looping through the new values, if the new value was in the old value array ($.inArray(val, arr) == -1 if not found) then that's the new value.  After that we're just using an attribute-equals selector to grab the element and get its .text().

If the value="" may contains quotes or other special characters that would interfere with the selector, use .filter() instead, like this:

$(this).children().filter(function() {
  return this.value == newVal[i];
}).text());

提交回复
热议问题