简体   繁体   English

如何通过JavaScript或JQuery或…来验证所有选择框是否都具有选定的选项?

[英]How can I verify that all select boxes have a selected option through JavaScript or JQuery or…?

I have 2 select boxes on a page with a variable number of options in them. 我在页面上有2个选择框,其中有多个选项。

For instance: 例如:

<fieldset>
    <label for="fizzwizzle">Select a Fizzwizzle</label>
    <select name="fizzwizzle" id="fizzwizzle" size="10">
        <option>Fizzwizzle_01</option>
        <option>Fizzwizzle_02</option>
        <option>Fizzwizzle_03</option>
        <option>Fizzwizzle_04</option>
    </select>
</fieldset>
<fieldset>
    <label for="fizzbaggot">Select a Fizzbaggot</label>
    <select name="fizzbaggot" id="fizzbaggot" size="10">
        <option>Fizzbaggot_01</option>
    </select>
</fieldset>

I would like to verify that both of these select boxes have a selected option. 我想验证这两个选择框是否都具有选定的选项。 My initial thought is to simply use JQuery but I can't seem to figure out how to do that. 我最初的想法是仅使用JQuery,但似乎无法弄清楚该怎么做。 My attempts so far have been futile but I have the following code that I think should work with the missing link. 到目前为止,我的尝试都是徒劳的,但是我认为以下代码可以与缺少的链接一起使用。

function verify_selectboxen_selection() {
    var allSelected = true;

    $('select').each(function() {
      /* if select box doesn't have a selected option */
            allSelected = false;
            break;
    });

    if (!allSelected) {
        alert('You must select a Job and a Disposition File.');
    }
    return allSelected;
}

Seems simple enough. 似乎很简单。 Thoughts? 有什么想法吗?

In jQuery you can use the :selected selector to get the total options selected. 在jQuery中,您可以使用:selected选择器来获取所选的全部选项。 This number out to match the number of select's themselves: 此数字与select自身的数目匹配:

if ($("select").length === $("option:selected").length) {
  // they match
}

I would like to verify that both of these select boxes have a selected option 我想验证这两个选择框是否都具有选定的选项

It's not possible for a (non- multiple ) select box not to have a selected option! (非multipleselect没有选择的选项是不可能的 If you don't declare selected on one of the option ​s, the browser will automatically select the first option. 如果您未在任何一个option上声明selected ,则浏览器将自动选择第一个选项。

So: return true; 所以: return true; :-) :-)

If you want to have an 'unselected' initial state, you'd have to include a no-option option for it, usually the first one: 如果要具有“未选择的”初始状态,则必须为其提供一个no-option option ,通常是第一个:

<select name="fizzbaggot">
    <option value="" selected="selected">(Select a fizzbaggot)</option>
    <option>foo</option>
    <option>baz</option>
    <option>bar</option>
</select>

Then you can check whether a different option to that one has been selected, either by saying: 然后,您可以通过以下方式检查是否选择了与该选项不同的选项:

$('select').each(function() {
    if ($(this).val()!=='')
        allSelected= false;
});

Or, if you might want to use the empty string as a valid value, you can just look at the index of the selected option: 或者,如果您想使用空字符串作为有效值,则只需查看所选选项的索引即可:

$('select').each(function() {
    if (this.selectedIndex===0)
        allSelected= false;
});

You can use the :selected selector for this. 您可以使用:selected选择器

var unselected = [] 
$('select').each(function(){
    if (0 == $(this).find('option:selected').length) {
        unselected.push(this.id);
    }
});

if (unselected.length != 0) {
    // unselected contains the ids of non-selected select boxes
}

Or you could check their values with val() . 或者,您可以使用val()检查它们的值。 This presumes that you have a default option with no value (ie empty string value). 这假定您有一个没有值的默认选项(即空字符串值)。

var unselected = [] 
$('select').each(function(){
    if ('' == $(this).val()) {
        unselected.push(this.id);
    }
});

if (unselected.length != 0) {
    // unselected contains the ids of non-selected select boxes
}
 function checkSelects() {
        return $("select :selected").length == $("select").length;
    }

alert(checkSelects());

暂无
暂无

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

相关问题 如何使用jquery检查所有选择框是否都选择了选项? - How to check if all select boxes has selected option using jquery? 如何获取所有没有使用 jQuery 选择选项的选择元素? - How do I get all select elements that do not have an option selected using jQuery? 使用JavaScript,我如何重置表单中的所有选择,以便选择每个选项的第一个选项? - Using JavaScript, how can I reset all the select's in my form so that the first option for each is selected? 如何使用 JavaScript\\JQuery 使用选择标记的选定选项的值设置隐藏输入标记的值? - How can I set the value of an hidden input tag with the value of the selected option of a select tag using JavaScript\JQuery? 如何使用jQuery选择选定选项的值? - How can I select the value of a selected option with jQuery? 如何使用 Javascript 或 Jquery 获取所选选项的值? - How can i get the value of the selected option using Javascript or Jquery? 如何使用 Jquery 将 2 个选择框中的所有选定值存储到数组中? - How to store all selected values from 2 select boxes into an array with Jquery? 如何检查是否已选择“动态创建的选择框” - How can I check if Dynamically created Select Boxes have been selected 如何使用此 JavaScript 使一个选择选项依赖于前一个选择选项? - How can I use this JavaScript to have one select option be dependent on the previous select option? 如何自动select儿童选项选择时选择父级选项JS,ZA565B176ECE81D91B5C08C08C39ADB65AF1Z - How can i auto select children option when selected parent option Js, Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM