Check if at least one checkbox is checked using jQuery

前端 未结 9 1159
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-31 11:31

I have five checkboxes. Using jQuery, how do I check if at least one of them is checked?



<
9条回答
  •  隐瞒了意图╮
    2020-12-31 12:20

    You can do the following way. Initially set a variable, lets say checked as false. Then set it to true if the following condition met. Use an if statement to check the variable. Take note: Here submit is the id of the button, main is the id of the form.

    $("#submit").click(function() {
      var checked = false;
      if (jQuery('#main input[type=checkbox]:checked').length) {
        checked = true;
      }
      if (!checked) {
        //Do something
      }
    });

提交回复
热议问题