简体   繁体   中英

Can't get checked checkboxes in jquery

I'm trying to fill an array but jquery isn't finding the checked checkboxes

 $('input[name="frequency"]:checked').map(function() { frequencies.push($(this).val()); }); 
 <div id="frequencyCheckboxes" class="btn-group" data-toggle="buttons" style="width:270px;"> <label class="btn btn-primary" for="cbm"> <input id="cbM" name="frequency" value="M" type="checkbox" autocomplete="off">M </label> <label class="btn btn-primary" for="cbW1"> <input id="cbW1" name="frequency" value="W1" type="checkbox" autocomplete="off">W1 </label> <label class="btn btn-primary" for="cbW2"> <input id="cbW2" name="frequency" value="W2" type="checkbox" autocomplete="off">W2 </label> <label class="btn btn-primary" for="cbW3"> <input id="cbW3" name="frequency" value="W3" type="checkbox" autocomplete="off">W3 </label> <label class="btn btn-primary" for="cbW4"> <input id="cbW4" name="frequency" value="W4" type="checkbox" autocomplete="off">W4 </label> <label class="btn btn-primary" for="cbW5"> <input id="cbW5" name="frequency" value="W5" type="checkbox" autocomplete="off">W5 </label> </div> 

Can you try below?

var frequencies = [];
$('input[name="frequency"]').click(function() {
  frequencies.push($(this).val());
});

i think using each instead map should works

$(input[name="frequency"]).click(function(){
    $('input[name="frequency"]:checked').each(function(i,el) {
      frequencies.push($(el).val());
    })
});

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