Okay, so I have some javascript code that is meant to do two things: tick checkboxes that have identical values (if checkbox with value "3" is checked, then any other with value "3" should also be checked). It is also meant to count how many checked boxes there are in total afterwards. This works perfectly.
$(".chkbox").on('change',function() {
var max_number =8;
var val = ($(this).val();
if ($(this).is(":checked")) {
$(":checkbox[value='" + val + "']").prop("checked", true);
} else {
$(":checkbox[value='" + val + "']").prop("checked", false);
}
var count = $("[type='checkbox']:checked").length;
if (count>=max_number){
alert("too many chosen!");
}
alert (count);
});
However, there are some checkboxes in the html which are not relevant, and are being counted erroneously. As such I have given the table which contains the relevant checkboxes the id of "test" and rewritten the javascript:
$(".chkbox").on('change',function() {
var max_number=8;
var val = ($(this).children("test")).val();
if (($(this).children("test")).is(":checked")) {
$(":checkbox[value='" + val + "']").prop("checked", true);
} else {
$(":checkbox[value='" + val + "']").prop("checked", false);
}
var count = $("[type='checkbox']:checked").length;
if (count>=max_number){
alert("too many chosen!");
}
alert (count);
});
This correctly counts the number of checkboxes - but it no longer ticks all checkboxes with the same value in test! How can I fix this?
Aucun commentaire:
Enregistrer un commentaire