JS获取checkbox的个数
var checkbox = document.getElementsByName("likes[]"); //此处通过此种方式才能获得多选框为数组。
//like为name = "like[]" , 获得时必须加上[]
var checked_counts = 0;
for(var i=0;i<checkbox.length;i++){
if(checkbox[i].checked){ //被选中的checkbox
checked_counts++;
}
}
alert(checked_counts);
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); } }
<input type="checkbox" name="tie_in[]" value="1" onClick="return checkData()" />