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()" /> 
  • 相关阅读:
    python数据类型详解
    python代码风格指南:pep8 中文翻译
    尝试一下: 仅加密已用磁盘空间
    Anaconda多环境多版本python配置指导
    Python科学计算(一)环境简介——Anaconda Python
    Python Collections里一些常用字典类的用法
    旋转链表
    Java 利用 split 方法切割字符串
    Java 语言设计中的部分共享策略
    JumpGame I
  • 原文地址:https://www.cnblogs.com/leezhxing/p/3338458.html
Copyright © 2011-2022 走看看