jQuery,检查是否选择了所有单选按钮组

2022-01-21 00:00:00 radio-button jquery javascript

我有几个单选按钮组,当它们被选中时我需要运行一个脚本.

我使用以下脚本来检查其中一个是否被选中,如果没有,则对其进行着色.

如何编写代码,以便在检查所有单选按钮组后,运行脚本.

检查单选按钮组是否被选中的代码:

$('.aQuestion').each(function(){if($(this).find('input[type="radio"]:checked').length > 0){警报(检查");}别的{警报(未检查");}});

单选按钮组(大约有 90 个):

提前致谢:D

解决方案

假设每个问题只有一个单选按钮组,您不需要迭代问题以找出它们都被选中:

p>

var $questions = $(".aQuestion");if($questions.find("input:radio:checked").length === $questions.length) {//全部检查}

jsFiddle 演示了上述内容.

I have several radiobuttongroups, and I need to run a script when they are checked.

I use the following script to check if one of them is checked, if not, then color it.

How can I make the code, so that when all of the radiobuttongroups are checked, then run script.

The code that checks if the radiobuttongroups are checked:

$('.aQuestion').each(function(){
  if($(this).find('input[type="radio"]:checked').length > 0)
    {
       alert("checked");
    }
  else
    {
       alert("not checked");
    }    
});

The radiobuttongroups (there is about 90 of them):

<div class='aQuestion' id='div1'>
    <STRONG>1. </STRONG>
    <STRONG>Question</STRONG></br>
    <INPUT TYPE='radio' NAME='grp1' VALUE='0'>answer 1</br>
    <INPUT TYPE='radio' NAME='grp1' VALUE='1'>answer 2</br>
    <INPUT TYPE='radio' NAME='grp1' VALUE='2'>answer 3</br>
    <INPUT TYPE='radio' NAME='grp1' VALUE='3'>answer 4</br>
    <INPUT TYPE='radio' NAME='grp1' VALUE='4'>answer 5
</div>

<div class='aQuestion' id='div2'>
    <STRONG>2. </STRONG>
    <STRONG>Question</STRONG></br>
    <INPUT TYPE='radio' NAME='grp2' VALUE='0'>answer 1</br>
    <INPUT TYPE='radio' NAME='grp2' VALUE='1'>answer 2</br>
    <INPUT TYPE='radio' NAME='grp2' VALUE='2'>answer 3</br>
    <INPUT TYPE='radio' NAME='grp2' VALUE='3'>answer 4</br>
    <INPUT TYPE='radio' NAME='grp2' VALUE='4'>answer 5
</div>

Thanks in advance :D

解决方案

Assuming there is only one radio button group per question, you don't need to iterate the questions in order to find out they're all selected:

var $questions = $(".aQuestion");
if($questions.find("input:radio:checked").length === $questions.length) {
    // All Checked
}

jsFiddle which demonstrates the above.

相关文章