问题描述
我的html看起来像这样
my html looks like this
<div id="div1">
<input type="radio" name="r1" value="v1" />
<input type="radio" name="r1" value="v2" />
<input type="radio" name="r1" value="v3" />
<input type="radio" name="r2" value="v1" />
<input type="radio" name="r2" value="v2" />
<input type="radio" name="r2" value="v3" />
<input type="radio" name="r3" value="v1" />
<input type="radio" name="r3" value="v2" />
<input type="radio" name="r3" value="v3" />
</div>
单选按钮是在我的html上动态生成的,因此在那个div中,我不知道我有多少个单选按钮.
radio buttons are dynamically generated on my html so in that div i don't know how many radio buttons i have.
我想确保用户在提交表单之前将为每个用户选择一个值,我如何检查div中的所有单选按钮是否都已选中一个值?
i want to make sure that the user will select a value for each one of them before he submits the form, how can i check that all radio buttons inside my div has a value checked?
谢谢
推荐答案
稍微重构HTML -将每个广播组都包裹在(例如)div中.然后,您可以执行以下操作来在用户提交表单时验证表单:
Restructure your HTML slightly - wrap each radio group in (say) a div. Then you can just do something like this to validate the form when the user submits it:
if ($('div:not(:has(:radio:checked))').length) {
alert("At least one group is blank");
}
当然,可以通过各种方式来调整它以适合您的HTML.这个想法来自找到所有未被选中的广播组
Of course this can be tweaked in various ways to suit your HTML. The idea was from Find all radio groups which haven't been selected
这篇关于jQuery:如何检查是否选中了div中的所有单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!