[英]How to check each group of checkbox is checked at least one
How to check each group of checkboxes is checked at least one on submit.如何检查每组复选框在提交时至少选中一个。 Using each function and every group should check at least on checkbox .problem is a dynamic checkbox with the same class name.使用每个函数和每个组应该至少检查复选框。问题是一个具有相同类名的动态复选框。 It would be more and more groups.So I need to give conditions as at least one checkbox should check.这将是越来越多的组。所以我需要给出条件,因为至少应该选中一个复选框。
Using each function: I ma checking whether the group of check box length is greater than zero but if any group check is checked submit is happening.使用每个函数:我会检查复选框长度组是否大于零,但是如果检查了任何组检查,则提交正在发生。
<div id='myContainer'>
<div class="usra">
<div class = "user1">
<fieldset >
<legend>allergies</legend>
this is first group with same class "allergies"
<div class="allergies">
<input type="checkbox" name="allergiesnme" value="Cat" />Cats <br />
<input type="checkbox" name="allergiesnme" value="Dog" />Dogs<br />
<input type="checkbox" name="allergiesnme" value="Bird" />Birds<br />
</div>
</fieldset>
<fieldset >
<legend>healthcondition</legend>
this is second group "healthcondition"
<div class="healthcondition">
<input type="checkbox" name="healthnme" value="Cat" />Cats <br />
<input type="checkbox" name="healthnme" value="Dog" />Dogs<br />
<input type="checkbox" name="healthnme" value="Bird" />Birds<br />
</div>
</fieldset>
</div>
<div class="user1">
<fieldset class="allergies">
<legend>allergies</legend>
<div class="allergies">
<input type="checkbox" name="allergiesnme" value="Cat" />Cats <br />
<input type="checkbox" name="allergiesnme" value="Dog" />Dogs<br />
<input type="checkbox" name="allergiesnme" value="Bird" />Birds<br />
</div>
</fieldset>
<fieldset class="healthcondition">
<legend>healthcondition</legend>
<div class="healthcondition">
<input type="checkbox" name="healthnme" value="Cat" />Cats <br />
<input type="checkbox" name="healthnme" value="Dog" />Dogs<br />
<input type="checkbox" name="healthnme" value="Bird" />Birds<br />
</div>
</fieldset>
</div>
</div>
<button type="button" id="clicksubmit">Click Me!</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--
4. We've got an element in the DOM, we've created a template, and we've
loaded the library - now it's time to build our Hello World app.
-->
<script>
var submitallergies = false; <!-- setting flag for one section ".allegies" -->
var submithealth = false; <!-- setting flag for one section ".healthcondition" -->
every group should check at least one checkbox
$("#clicksubmit")
.click(function() {
alert("inside");
using each
function of each ".user1"
$('.user1')
.each(function() {
alert("user each");
$('.allergies')
.each(function() {
var allergiescheck = $(".allergies input[name^=allergiesnme]:checked")
.length;
alert("allergies each");
if (allergiescheck > 0) {
alert("allergiescheck is greater than zero");
submitallergies = true;
} else if (allergiescheck == 0) {
return false;
}
});
$('.healthcondition')
.each(function() {
//alert(this.value +"health value");
alert("healthcondition each");
var healthcheck = $(".healthcondition input[name^=healthnme]:checked")
.length;
if (healthcheck > 0) {
alert("healthcheck is greater than zero");
submithealth = true;
} else if (healthcheck == 0) {
return false;
}
});
if (submitallergies == true) {
if (submithealth == true) {
alert("submitted");
} else {
return false;
}
}
});
});
</script>
Any help or suggestion are appreciated.任何帮助或建议表示赞赏。
You can try this:你可以试试这个:
$("#clicksubmit")
.on('click', function(){
var flag;
$('.user1')
.each(function(){
flag = false;
if(!$('.allergies input', $(this)).is(':checked'))
return false;
if(!$('.healthcondition input', $(this)).is(':checked'))
return false;
flag = true;
});
if (flag)
alert("submitted");
else
alert("Not submitted");
});
Here is the UPDATED FIDDLE
.这是UPDATED FIDDLE
。
Edit 1: Return true or false编辑 1:返回真或假
Currently, return
is returning false
.目前, return
返回false
。 So, it will exit the the each
loop.因此,它将退出each
循环。 If you need it to continue ie, go to the next iteration just return true
.如果您需要它继续,即,转到下一次迭代只需return true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.