get multiple values from dropdownlist in JavaScript

后端 未结 3 1949
醉酒成梦
醉酒成梦 2021-01-14 05:10

How can I get the values selected in the drop-down list, using a JavaScript function? User can select multiple values from both the elements. Following are the elements

相关标签:
3条回答
  • 2021-01-14 05:51
    var fld = document.getElementById('icOptions');
    var values = [];
    for (var i = 0; i < fld.options.length; i++) {
      if (fld.options[i].selected) {
        values.push(fld.options[i].value);
      }
    }
    // do something with values
    
    0 讨论(0)
  • 2021-01-14 06:02

    An ES6/functional style alternative to the accepted answer:

    const values = Array.apply(null, e.target.options)
      .filter(option => option.selected)
      .map(option => option.value);
    
    0 讨论(0)
  • 2021-01-14 06:07

    Really Awesome Library for your need using jQuery or Prototype http://harvesthq.github.com/chosen/ Have a look at it.

    It supports select & multiselect in a really nice way

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