2015-09-27 139 views
2

如何检查每组复选框是否至少被检查一次。 使用每个功能和每个组应至少检查复选框。问题是具有相同类名的动态复选框。这将是越来越多的群体。所以我需要给出条件,至少有一个复选框应该检查。如何检查每组复选框是否至少被选中一个

使用每个函数:我可以检查复选框长度组是否大于零,但是如果检查到任何组检查正在发生提交正在发生。

<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> 

任何帮助或建议表示赞赏。

+0

是否要验证每个组中至少有一个验证,或者只有一个来自所有组。 –

+0

:如果选择“无”并且成员选择过敏症之一,则“无”需要被取消选中。 同样,当少数过敏症被选中时,成员选择'无',那么所有过敏症,选定必须取消选中。 – user3222157

回答

1

你可以试试这个:

$("#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"); 
    }); 

这里是UPDATED FIDDLE

编辑1:返回TRUE或FALSE

目前,return正在恢复false。所以,它将退出each循环。如果您需要继续操作,请转至下一个迭代return true

+0

如果有任何一个组被检查,两个条件都提交正在发生..我的要求是至少一个被检查的应该是所有组。 – user3222157

+1

@ user3222157检查更新。 – Beginner

+0

伟大的:-)谢谢 – user3222157