jQuery, check to see if all radiobutton groups are selected(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,检查是否选择了所有单选按钮组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jQuery,检查是否选择了所有单选按钮组


- addEventListener 在 IE 11 中不起作用 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07