简体   繁体   中英

How to validate at least one Checkbox is checked using JQuery?

Hear is my code:

<div class="reminderDiv">
    <input type="checkbox" class="require-one" id="lmrText"  name="lmrText"><label>Text Message</label>&nbsp;&nbsp;&nbsp;
    <input type="checkbox" class="require-one" id="lmrPhone" name="lmrPhone"><label>Phone</label>
    <input type="checkbox" class="require-one" id="lmrEmail" name="lmrEmail"><label>Email</label>
</div>

Using JQuery, how can I determine if at least one checkbox is selected?

$(".reminderDiv :checked").length > 0

如果至少选中一个复选框,则为true

if($('.require-one:checked').length > 0)
{
   // at least one checkbox was checked
}
else
{
  // no checkbox was checked
}

JSFIDDLE

if($('input[type=checkbox]').is(':checked')==true) {
    alert("Your data");
}
if($('.require-one:checked').size() === 0){
{
    //your code here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM