Javascript 如何选择多选的所有选项,顺序不应改变

Javascript 如何选择多选的所有选项,顺序不应改变,javascript,jquery,Javascript,Jquery,如何在java脚本中选择multiselect框的所有选项?使用each循环所选项目并将值保存在数组中 var selectedVal =[]; $('select').find('option:selected').each(function(){ selectedVal.push($(this).val()); }); 首先,选择使用选定的所有选项 jQuery('select#mySelect option');//mySelect is id of your multiple sele

如何在java脚本中选择multiselect框的所有选项?

使用each循环所选项目并将值保存在数组中

var selectedVal =[];
$('select').find('option:selected').each(function(){
selectedVal.push($(this).val());
});

首先,选择使用选定的所有选项

jQuery('select#mySelect option');//mySelect is id of your multiple selectbox
然后使用.each循环并选择每个选项:

jQuery('select#mySelect option').each(function(index){
   console.info($(this).val() );//gets the value attribute if you have value attribute assigned to options
   console.info($(this).text() );//gets the text of each option while looping

   $(this).prop('selected',true);//this makes each option selected
});
试试这个

$('#your_selectbox_id').find('option').each(function() {
            $(this).attr('selected', 'selected');
});

$select option.prop'selected',truewhy put in array?当您将select设置为select的所有子级时?让选项被选中或不被选中,所有选项都应该按照相同的顺序从上到下依次出现。这个问题充其量是模糊的,我选择所选选项的值并将其保存在数组中以备将来使用,但我希望所有可用选项从上到下逐个保存。。很明显@madalin ivascuYes@madalin ivascu你是对的……答案就是这样。非常棒@Vaidehi Hirani,这是我第一次体验stackoverflow。。谢谢。