How to check multiple checkboxes checked using jquery?

前端 未结 5 1777
别那么骄傲
别那么骄傲 2021-01-19 02:27

i am new to the jquery, it is quite interesting, but i am having a little problem, i am populating multiple checkboxes from database using foreach loop like this,



        
5条回答
  •  天命终不由人
    2021-01-19 03:12

    If you want at least one checkbox checked, you can use this

    var somethingChecked = false;
    $("input[type=checkbox]").each(function() {
      if(this).is(':checked')) {
        somethingChecked = true;
      }
    });
    if(!somethingChecked) {
      alert("You haven't checked anything yet");
    }
    

    What this does is initialize a variable to false. Then the script loops through all inputs of type checkbox. If the item is checked, set the variable to true. Finally, check if the variable is still false. If it is, then show an error.

提交回复
热议问题