【问题标题】:JQuery validation for checkbox复选框的 JQuery 验证
【发布时间】:2014-09-10 08:21:40
【问题描述】:

我知道很多人会说这是重复的问题,但我尝试了所有选项但没有运气... 我有一个表单,它有大约 25 个复选框,在提交时,验证应该检查至少一个选择

这是我的复选框元素...

<input type="checkbox" class="check" name="check[]" value="1">
<input type="checkbox" class="check" name="check[]" value="2">
<input type="checkbox" class="check" name="check[]" value="3">
<input type="checkbox" class="check" name="check[]" value="4"> 
<input type="checkbox" class="check" name="check[]" value="5">

这是我的 jquery:

 <script>

 jQuery(".exportSelected").click(function(){
    $('#export').prop('checked', true);

    var checkedAtLeastOne = false; 
     if($('.check').prop( "checked" )==true)
     {
      checkedAtLeastOne == true;
     }        

    if (checkedAtLeastOne == true) {
     jQuery(".submitDel").trigger("click"); 
     }
     else {
    alert ("Atleast select one to Export")
    }  
  });
 </script>

【问题讨论】:

    标签: jquery validation checkbox


    【解决方案1】:

    这将返回选中的复选框数:

     $("input.check:checked").length
    

    只需给出以下条件,例如选中多个复选框:

    if( $("input.check:checked").length > 0) {
     //Your Code
    }
    

    另外,您需要在Document ready 事件之后触发Click event

    ** DEMO Here**

    希望这能解决问题。

    【讨论】:

    • 必须在ready事件下处理click事件。我更新了我的帖子。
    【解决方案2】:

    使用length 查找否。选中的复选框。如果它为零,则没有一个被选中

    jQuery(".exportSelected").click(function(){
                if($('.check:checked').length>0){  // to check if check box checked more then one
                  $(".submitDel").trigger("click"); 
                }else{
                 alert ("Atleast select one to Export")
                }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多