最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if all select boxes has selected option using jquery? - Stack Overflow

programmeradmin0浏览0评论

I have 7 simple select boxes:

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

How can I check if ALL the select boxes have something selected rather than the default option?

I have 7 simple select boxes:

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

How can I check if ALL the select boxes have something selected rather than the default option?

Share Improve this question edited Sep 13, 2016 at 8:00 Mohammad 21.5k16 gold badges57 silver badges85 bronze badges asked Sep 13, 2016 at 7:33 Alexandru VlasAlexandru Vlas 1,4253 gold badges19 silver badges31 bronze badges 4
  • where is option value? – yash Commented Sep 13, 2016 at 7:36
  • Set the value of the select input.... If value attribute is not specified, text will be treated as value.. – Rayon Commented Sep 13, 2016 at 7:36
  • You get the Element in any number of ways, such as document.getElemnetById('HTMLidAttribute') then it could be as simple as testing for Element.selected. – StackSlave Commented Sep 13, 2016 at 7:37
  • then you can check that via jquery at here – yash Commented Sep 13, 2016 at 7:38
Add a ment  | 

3 Answers 3

Reset to default 3

You can find selected option that are disabled, if length==0 then no default element are selected.

if($('option[disabled]:selected').length == 0){
   // ALL the select boxes have something selected rather than the default option
}

Try this. Get value of select and check if its null and increment the count if its not.

$(function(){
    $('#btn').click(function(){
    var count = 0;

    $('select').each(function(){
      
    if($(this).val() != null){
       count ++; 
     }
     
  })
    if(count == 4) {
      console.log('all selected');
    }
  })
  
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<button id="btn">Check</button>

This works for me

            var count = 0;
            $("select").each(function() {
                if(this.value === ''){
                    count++
                }
            });
            if (count === 0) {
                // all select boxes has selected option 
            }
发布评论

评论列表(0)

  1. 暂无评论