JS获取checkbox的个数

 

本文算是转载自网络,当时用了他的函数,如今想总结一下,但忘了原文地址了j_0068.gifhtml

=======================================================================web

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);

 

============================================================函数

我作的是每点击一下多选框就判断当前checked个数是否超过某个数值spa

============================================================code

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);
       }
} 

 

要使函数checkdata()每次点击都发挥做用,须要在checkbox框中添加onclick事件:orm

 
 <input type="checkbox" name="tie_in[]" value="1" onClick="return checkData()" /> 
相关文章
相关标签/搜索