检查每个组中至少选择了一个单选按钮(使用jQuery) [英] checking at least one radio button is selected from each group (with jquery)
问题描述
我正在表单上动态拉出并显示单选按钮问题(通过jquery选项卡分为不同的部分).
I'm dynamically pulling and displaying radio button quesitons on a form (separated into different sections by jquery tabs).
因此,共有2个,3个,4个,5个等单选按钮,它们具有相同的名称"但ID不同.
So there are lots of 2, 3, 4, 5 etc radio buttons together, which have the same 'name' but different ids.
当您在各部分之间单击时,我要检查是否为每个组至少选择了一个单选按钮.
When you hit next between the sections, I want to check that at least one radio button has been selected for each group.
(加载表单时,默认情况下不选中单选按钮,而必须采用这种方式.)
( When the form loads, the radio buttons have none checked by default and it has to be this way. )
我可以循环浏览当前部分的单选按钮,但是我不确定如何轻松地检查每个组是否已被选中?
I can loop through the current section's radio buttons but I'm not sure how to easily check that one has been checked for each group?
推荐答案
在元素之间没有任何关系或如何查找组名的情况下,我只能给您一个指针,但这应该可以工作(触发了检查通过单击id="nextButton"
的元素,但是显然可以将其自定义为任何其他适当的事件,例如$('form').submit()
):
In the absence of any relationship between elements, or how to find the group names, I can only give you a pointer, but this should work (the check's triggered by clicking on the element of id="nextButton"
, but obviously this can be customised to any other appropriate event, such as $('form').submit()
, for example):
$(document).ready(
function(){
$('#nextButton').click(
function(){
var radioName = something; // use this if there's a relationship between the element
// triggering this check and the radio buttons
if ($('input[name='+ radioName +']:checked').length) {
// at least one of the radio buttons was checked
return true; // allow whatever action would normally happen to continue
}
else {
// no radio button was checked
return false; // stop whatever action would normally happen
}
}
);
}
);
这篇关于检查每个组中至少选择了一个单选按钮(使用jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!