multiple select element - onchange

后端 未结 9 1984
栀梦
栀梦 2021-02-18 22:50

I have a select element that allows for multiple selections. I\'d like to display the selected values in another part of the page (in a div or something) as the user makes chang

相关标签:
9条回答
  • 2021-02-18 23:24

    You can use the :selected Selector of jQuery instead, but I believe that under the hood, jQuery does a loop on the selected = true.

    0 讨论(0)
  • 2021-02-18 23:26
    element.addEventListener('click', function(){alert(this.value)})
    

    This is a solution in JS, you can port it over to jQuery pretty easily. The idea is to add a click listener to each option in the selection. This will not work in IE8 and below because of addEventListener, there are ways to get around this though.

    I think this is a better approach then having to reiterate over the list. You will have to have a listener attached to each option though.

    0 讨论(0)
  • 2021-02-18 23:29

    If you could use jQuery it might be as easy as:

    $('select').change(function() {alert($(this).val())})
    
    0 讨论(0)
提交回复
热议问题