zoukankan      html  css  js  c++  java
  • JS获取checkbox的个数

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

    =======================================================================

    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个数是否超过某个数值

    ============================================================

    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事件:

     
     <input type="checkbox" name="tie_in[]" value="1" onClick="return checkData()" /> 
  • 相关阅读:
    SQL注入过滤
    ASP.NET长文章分页
    简单的权限管理类
    不错的面试题
    【转载】【重要】Ubuntu Linux 下 Ffmpeg 及 Mencoder 安装使用小结
    回到xwindows
    suse11 linux不自动启动xwindows
    flash的几种工具
    mencoder和ffmpeg参数详解
    ffmpeg和Mencoder使用实例小全
  • 原文地址:https://www.cnblogs.com/leezhxing/p/3338458.html
Copyright © 2011-2022 走看看