简体   繁体   English

如何使用jquery检查所有选择框是否都选择了选项?

[英]How to check if all select boxes has selected option using jquery?

I have 7 simple select boxes:我有 7 个简单的选择框:

<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?如何检查所有选择框是否都选择了某些内容而不是默认选项?

You can find selected option that are disabled, if length==0 then no default element are selected.您可以找到已禁用的选定选项,如果 length==0 则不选择默认元素。

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.获取 select 的值并检查它是否为空,如果不是则增加计数。

 $(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.com/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 
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过JavaScript或JQuery或…来验证所有选择框是否都具有选定的选项? - How can I verify that all select boxes have a selected option through JavaScript or JQuery or…? 如何验证表单字段集是否已选中所有(如果有的话)选择框? - How to validate that a form fieldset has all, if any, select boxes selected? 如何使用 Jquery 将 2 个选择框中的所有选定值存储到数组中? - How to store all selected values from 2 select boxes into an array with Jquery? 使用jQuery选中特定的Tbody中的所有复选框 - Select all check boxes in specific tbody using jquery jQuery Multi Select Option-检查是否选择了一个选项 - jQuery Multi Select Option - Check if a option is selected or not 检查是否在不使用jquery的情况下未在选择框中明确选择任何选项? - Check that no option was explicitly selected in a select box without using jquery? 如何使用jQuery禁用选择框中的非选定选项? - How to disable non selected options in select boxes using jQuery? jQuery:如何检查是否在选择框中明确选择了 NO 选项 - jQuery : How to check if NO option was explicitly selected in a select box 检查页面中的所有下拉列表是否都具有选定的选项 - Check if all dropdowns in page has a selected option 检查是否使用jQuery选择了选项,如果没有选择默认值 - Check if option is selected with jQuery, if not select a default
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM