如何确定在多个<select>?</select>中选择的选项

时间:2014-07-29 06:14:51

标签: javascript jquery html html-select

我有<select>

 <select id="week" multiple="multiple" >
    <option value="1">Monday</option>
    <option value="2">Tuesday</option>
    <option value="3">Wednesday</option>
    <option value="4">Thursday</option>
    <option value="5">Friday</option>
    <option value="6">Saturday</option>
    <option value="7">Sunday</option>
</select>

我如何知道jQuery或JS中是否选择了每个<option>

3 个答案:

答案 0 :(得分:2)

尝试使用:selected选择器来抓取所选的选项元素

$('#week option:selected').each(function(){
   //Your code to manipulate things over selected option elements
});

答案 1 :(得分:1)

您需要使用.is:

 $('#week option').each(function() {
        if($(this).is(':selected')){
      //code
   }
 });

答案 2 :(得分:0)

您可以使用所选和总选项的length属性,请参阅下面的jquery:

var totalOptions = $('#week option').length; // all options available
var selectedOptions = $('#week option:selected').length;// selected options

if(totalOptions == selectedOptions)
 alert('All options selected');
else
 alert('Not all options selected');