JQuery - check if at least one checkbox is checked

I have the following HTML form and it can have many checkboxes. When the submit button is clicked, I want the user to receive a javascript warning to check at least one checkbox if none of them are checked. Is there an easy way to do this using 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> 
+57
javascript jquery
Apr 21 '10 at 15:48
source share
7 answers
 if(jQuery('#frmTest input[type=checkbox]:checked').length) { … } 
+89
Apr 21 '10 at 15:51
source share
 $('#frmTest input:checked').length > 0 
+32
Apr 21 '10 at 15:50
source share
 $("#frmTest").submit(function(){ var checked = $("#frmText input:checked").length > 0; if (!checked){ alert("Please check at least one checkbox"); return false; } }); 
+18
Apr 21 '10 at 15:51
source share
 $("#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); } }); 
+9
May 8 '13 at 8:31
source share
 $('#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 these flags is checked.

+5
Sep 15 '10 at 16:34
source share

 $('#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> 
+5
Apr 10 '17 at 20:53 on
source share
 if($("#checkboxid1").is(":checked")) || ($("#checkboxid2").is(":checked")) || ($("#checkboxid3").is(":checked"))) { //Your Code here } 

You can use this code to make sure that at least one checkbox is selected.

Thank!!

0
Jun 05 '19 at 10:43 on
source share



All Articles