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

jQuery, check to see if all radiobutton groups are selected(jQuery,检查是否选择了所有单选按钮组)
本文介绍了jQuery,检查是否选择了所有单选按钮组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

$('.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.

这篇关于jQuery,检查是否选择了所有单选按钮组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)