Get values from multiple selections in SELECT

后端 未结 3 826
清歌不尽
清歌不尽 2021-01-14 04:33

I have a selection box that allows you to select multiple options. I need to access all the selected values with JavaScript - possible a array of values?

相关标签:
3条回答
  • 2021-01-14 05:20
    var values = [];
    $('#my_select option:selected').each(function(i, selected){
        values[i] = $(selected).attr('value');
    });
    
    0 讨论(0)
  • 2021-01-14 05:21

    I would use $.map:

    var values = $('#mySelect option:selected').map(function() {
        return this.value; // or $(this).val()
    }).get();
    
    0 讨论(0)
  • 2021-01-14 05:31

    This is the best way to get an array of the selected values back:

    $("#mySelect").val(); // Return an array of the selected options values
    

    This assumes that multiple="mutliple" and size is greater than one on the select element.

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