javascript jQuery - 检查是否选择了多个单选按钮组,然后返回未选择的按钮组的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20871879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - Check if multiple radio button groups are selected, then return name of those who aren't
提问by RedHawkDK
I have +90 questions in a survey. Each question has 5 choices. The questions is built by database information.
我在调查中有 +90 个问题。每个问题有5个选择。问题是由数据库信息构建的。
I need to check if all the questions were answered, and if one of them wasn't, then this should be alerted.
我需要检查是否所有问题都得到了回答,如果其中一个没有得到回答,那么应该提醒这一点。
They are all divided into radio button groups, and I would like to use jQuery to check.
它们都分为单选按钮组,我想使用jQuery来检查。
The if statement won't work, the only think that gets alerted is: " is NOT checked!"
if 语句不起作用,唯一得到警告的想法是:“未检查!”
<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>
( And there are 8 more those questions as above )
(还有 8 个以上的问题)
<script>
jQuery('#submit').click(function(event)
{
event.preventDefault();
for(i=1;i<=10;i++)
{
var currentGroup = "grp" + i;
if($("input[name=currentGroup]:checked").val())
{
alert(currentGroup + ' is checked!');
}
else
{
alert(currentGroup + ' is NOT checked!');
}
}
});
</script>
Thanks in advance. And if anyone has an idea how to make users browser view jump to the questions that weren't answered, then I would like to hear that too ;)
提前致谢。如果有人知道如何让用户浏览器视图跳转到未回答的问题,那么我也想听听;)
采纳答案by Rajaprabhu Aravindasamy
You have to traverse each div with class aQuestion
by using .each()
and you need to check whether any radio buttons are checked inside that of element by using .find()
.
你必须穿越带班每个格aQuestion
使用.each()
,你需要检查是否有任何单选按钮,通过使用内部的元素的检查.find()
。
Try,
尝试,
$('.aQuestion').each(function(){
if($(this).find('input[type="radio"]:checked').length > 0)
{
alert("checked");
}
else
{
alert("not checked");
}
});
As mplungjan suggested you can also use,
正如 mplungjan 建议你也可以使用,
$('.aQuestion').each(function(){
$(this).toggleClass("didntmakechoice",$(this).find('input[type="radio"]:checked').length == 0);
});