多个组或单选按钮
<h1>Question 1</h1>
<input type="radio" name="radio1" value="false" />
<input type="radio" name="radio1" value="true" />
<h1>Question 2</h1>
<input type="radio" name="radio2" value="false" />
<input type="radio" name="radio2" value="true" />
如何在 jQuery 中检查每个组中的单选按钮是否已选中?
谢谢。
最佳答案
这是我的做法:
var all_answered = true;
$("input:radio").each(function(){
var name = $(this).attr("name");
if($("input:radio[name="+name+"]:checked").length == 0)
{
all_answered = false;
}
});
alert(all_answered);
这样您就不需要依赖兄弟标签或父标签。如果您的单选按钮与任何标签混合在一起,它仍然可以工作。您可以play around with it .
关于jquery - 如何在 jQuery 中检查每个组中的单选按钮是否被选中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3553197/