Javascript 我可以使用 jQuery 检查是否至少选中了一个复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2684434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:27:34  来源:igfitidea点击:

Can I use jQuery to check whether at least one checkbox is checked?

javascriptjquery

提问by Jake

I have the following HTML form which can have many checkboxes. When the submit button is clicked, I want the user to get a javascript alert to check at least one checkbox if none are checked. Is there an easy way to do this using jQuery?

我有以下 HTML 表单,其中可以有许多复选框。单击提交按钮时,如果没有选中任何复选框,我希望用户收到一个 javascript 警报以检查至少一个复选框。有没有一种简单的方法可以使用 jQuery 做到这一点?

<form name = "frmTest" id="frmTest">
  <input type="checkbox" value="true" checked="true" name="chk[120]">
  <input type="checkbox" value="true" checked="true" name="chk[128]">
  <input type="checkbox" name="chk[130]">
  <input type="checkbox" name="chk[143]">
  <input type="submit" name="btnsubmit" value="Submit">
</form>

回答by Quentin

if(jQuery('#frmTest input[type=checkbox]:checked').length) { … }

回答by Matthew Flaschen

$('#frmTest input:checked').length > 0

回答by Jon

$("#frmTest").submit(function(){
    var checked = $("#frmText input:checked").length > 0;
    if (!checked){
        alert("Please check at least one checkbox");
        return false;
    }
});

回答by chithon

$("#show").click(function() {
    var count_checked = $("[name='chk[]']:checked").length; // count the checked rows
        if(count_checked == 0) 
        {
            alert("Please select any record to delete.");
            return false;
        }
        if(count_checked == 1) {
            alert("Record Selected:"+count_checked);

        } else {
            alert("Record Selected:"+count_checked);
          }
});

回答by Domingo

$('#fm_submit').submit(function(e){
    e.preventDefault();
    var ck_box = $('input[type="checkbox"]:checked').length;
    
    // return in firefox or chrome console 
    // the number of checkbox checked
    console.log(ck_box); 

    if(ck_box > 0){
      alert(ck_box);
    } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name = "frmTest[]" id="fm_submit">
  <input type="checkbox" value="true" checked="true" >
  <input type="checkbox" value="true" checked="true" >
  <input type="checkbox" >
  <input type="checkbox" >
  <input type="submit" id="fm_submit" name="fm_submit" value="Submit">
</form>
<div class="container"></div>

回答by namklabs

$('#frmTest').submit(function(){
    if(!$('#frmTest input[type="checkbox"]').is(':checked')){
      alert("Please check at least one.");
      return false;
    }
});

is(':checked') will return true if at least one or more of the checkboxes are checked.

如果至少选中了一个或多个复选框,is(':checked') 将返回 true。