JS gets the number of checkboxes selected by multiple checkboxes.
var checkbox = document.getElementsByName("likes[]"); //This way can you get the multi-checkbox as an array. //Like is name = "like[]", and when obtaining, you must add []var checked_counts = 0;for(var i=0;i<checkbox.length;i++){if(checkbox[i].checked){ //The selected checkboxchecked_counts++;}}alert(checked_counts);What I do is to determine whether the current checked number exceeds a certain value for each click of the multi-checked box
function checkDate(){ var n = $("#cart_q_num").val(); var checkedCount=0; var checkbox = document.getElementsByName("tie_in[]"); //alert(checkbox.length); for(var i=0;i<checkbox.length ;i ++){ if(checkbox[i].checked){ checkedCount++; } } //alert(checkedCount); if(checkedCount>n){ alert("The quantity of the gifts should equal to the quantity of the sunglasses set."); return false; }else{ $("#free_pro_selected_num").html(checkedCount); }}To make the function checkdata() work every time you click, you need to add the onclick event in the checkbox box:
<input type="checkbox" name="tie_in[]" value="1" onClick="return checkData()" />
The above simple example of JS obtaining checkbox is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.