0

在下面的示例中,我希望选择“radios”列表的所有“:checked”单选按钮。如何?

var radios = someDiv.find("input[type=radio]");
if (radios.length > 0)
{
    // I wish to find all the checked radio buttons in the
    // "radios" list - this doesn't work:
    var checkedRadios = radios.find(":checked");

    if (checkedRadios.length == 0)
        alert("You have to select an option stoopid");
}
4

3 回答 3

2

要使您的代码按原样工作,您只需要替换findfilter

var checkedRadios = radios.filter(":checked");

除非您有任何特定理由进行两步选择,否则我建议您使用一个选择器来选择单选按钮

var checkedRadios = someDiv.find("input[type=radio]:checked");
于 2012-09-26T14:56:05.467 回答
1

试试这个

if(someDiv.find("input[type=radio]:checked").length == 0)
  alert("You have to select an option stoopid");

如果你知道 div id

if($('#someDivId').find("input[type=radio]:checked").length == 0)
      alert("You have to select an option stoopid");

如果你知道 div 类,但它会在这个类的所有 div 上运行

 if($('.someDivClass').find("input[type=radio]:checked").length == 0)
          alert("You have to select an option stoopid");
于 2012-09-26T14:56:34.780 回答
0

var radios = someDiv.find("input[type=radio]:checked");应该管用


更新

var radios = someDiv.find("input[type=radio]");

// radios are there
if (radios.length > 0)
{
    var numChecked = 0;
    radios.each(function() {
        if($(this).is(':checked')) numChecked++;
    });

    if (numChecked == 0) 
        alert("You have to select an option stoopid");
}
于 2012-09-26T14:55:07.653 回答