English 中文(简体)
我如何通过JavaScript或JQuery验证所有选择框是否有已选择的选项?
原标题:How can I verify that all select boxes have a selected option through JavaScript or JQuery or...?

我在一页上有2个选箱,其中可选择数。

例如:

<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>

我想确认这两个选择框都有选定的选项。我的初步想法是简单地使用 JQuery,但我似乎无法弄清楚如何做到这一点。到目前为止,我的尝试都是徒劳的,但我有以下代码,我认为应该能够解决问题。

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;
}

种子足够简单。 想法?

最佳回答

在jQuery中,可以使用:selected选择器来获取选择的选项总数。这个数字应该与选择自身的数量匹配:

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

我想确认这两个选择框都有一个已选择的选项。

一个(非多选)下拉框不可能没有选中的选项!如果您没有在其中一个选项上声明“selected”,浏览器会自动选择第一个选项。

所以:返回真; :-)

如果您想要一个“未选择”的初始状态,您必须为其包括一个无选项选项,通常是第一个:

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

然后你可以检查是否已选择不同的选项,可以说:

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

或者,如果您想将空字符串用作有效值,则可以查看所选选项的索引:

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

您可以使用: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
}

或者,你可以用val()。 假设你有一个没有价值的缺省方案(即空洞价值)。

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());




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签