如图所示: 这个用table写的 checkbox放在<td>标签里,我之前写的那个是table里全部的复选框,多选了会提示只能选几个。代码是这样的:<script type="text/javascript">
    function setCheckValue() {
        var obj = document.getElementById("MainContent_hdTimesCheckValue");
        var cheked_num=0; //准备选择的checkbox
        var hadcheked_num=0; //已经选择的checkbox
        var count_num = <%=lg.ToInt(practiceCountTimes)%>;
        if (obj != undefined && obj != null) {
            var chk = document.getElementsByName("chkPracticeTimes");
            if (chk != undefined && chk != null) {
                obj.value = "";
                if (chk.length == undefined) {
                    if (chk.checked) obj.value = chk.valueOf;
                } else {
  
                    for (var i = 0; i < chk.length; i++) {
                        if (chk[i].checked) {
                            cheked_num+=1;
                            if (count_num!=0&&cheked_num>count_num) {
                                alert("最多只能选"+count_num+"个!");
                                event.srcElement.checked = false;
                              //  cheked_num--;
                                i--;
                            }
                            obj.value += "|" + chk[i].value;                      
                        }  
                    }                }
            }
        }    }</script>
现在是想要根据日期限制每行复选框的的选项,就是table里每行的复选框都限制不一样的,选多了会提示只能选多少个的那种。已经折腾几天了,有哪位js高手帮帮忙吧  ,在痛苦中!!我现在只有这么多分了,因为比较急,所以没去换分呢,希望帮帮忙吧,谢谢啦!!JavaScriptCheckBoxtable

解决方案 »

  1.   

    给你个思路吧,你在每行的日期 所在td加个属性,即此行的限制个数
    然后点击一行的复选框的时候 取出日期td的那个属性比较下就ok了
      

  2.   


    我就是在日期那里放了一个隐藏的值,这个值就是限制每行的个数的<td class='tdDate'>2013-6-10 <input type='hidden' name='hPriceDateTime' id='hPriceDateTime' value='1'></td> 然后  复选框是这样的<td class='tdDate'>2013-6-10 <input type='hidden' name='hPriceDateTime' id='hPriceDateTime' value='1'></td>
    <td  class='tdDisable'><input type='checkbox' name='chkPracticeTimes' id='chkPracticeTimes' value='2013-06-10 08:00' onclick='setCheckValue()' disabled ></td>  我是想要遍历这个table  然后根据每行所选的复选框值得总数来比较,可是我遇到瓶颈了,就是不知道怎么从 table的<td>里获取input控件的值,和这一行里再遍历一下checkbox。 
      

  3.   

    你给每行的tr设个name或者id,以此取得那一行,
    var getTr01=document.getElementById("tr1");
    var getTr01Input=getTr01.getElementsByTagName("input");然后再便利他的checked属性值,获取个数和tr里隐藏的个数比较。不知道我又没有把你的需求理解错
      

  4.   

    <html class="">
    <head>
    <meta charset="UTF-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
    <script>
    $(function(){
    $("table.table1 input[type='checkbox']").click(function(){
    var tr=$(this).closest("tr");
    var maxcheckedLen=~~(tr.attr("maxChecked"));
    var checkedLen=$("input[type='checkbox']:checked",tr).length;
    if(maxcheckedLen<checkedLen){
    alert("超过最大限制!");
    $(this).prop("checked",false);
    }

    });
    });
    </script>
    </head>
    <body><table width="977" border="1" class="table1">
    <tr maxChecked="2">
    <td><input type="checkbox" ></td>
    <td><input type="checkbox" ></td>
    <td><input type="checkbox" ></td>
    <td><input type="checkbox" ></td>
    <td><input type="checkbox" ></td>

    </tr>
    <tr maxChecked="3">
    <td><input type="checkbox" ></td>
    <td><input type="checkbox" ></td>
    <td><input type="checkbox" ></td>
    <td><input type="checkbox" ></td>
    <td><input type="checkbox" ></td>

    </tr>
    </table></body>
    </html>不知道我有没有把你的需求理解错
      

  5.   

    嗯嗯,这正是我想要的效果,但是我单独放在一个html里可以用,可是放到我的代码是却没有限制的效果啊,我那些表格是从后台里动态生成的,这有影响吗?
      

  6.   

     用的asp.net c#的  我现在是遍历所有的checkbox的值,然后用分割符“|”把值分开存到一个字段里的,然后那个表示绑定一个事件后再显示的,所以我怀疑是不是页面没刷新的问题造成的,你用的那个jquery限制的同时,能不能获取其值啊
      

  7.   

    <html class="">
    <head>
    <meta charset="UTF-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
    <script>
    $(function(){
        $("table.table1 input[type='checkbox']").click(function(){
            var tr=$(this).closest("tr");
            var maxcheckedLen=~~(tr.attr("maxChecked"));
            var checkedLen=$("input[type='checkbox']:checked",tr).length;
            if(maxcheckedLen<checkedLen){
                alert("超过最大限制!");
                $(this).prop("checked",false);
            }
             
        });
    });
    function getValue(){
    var values=[];
    $("input[type='checkbox']:checked").each(function(){
    values.push($(this).val());
    });
    alert(values.join('|'));
    }
    </script>
    </head>
    <body>
    <input width="80px"  type="button" onclick="getValue();" value='GetValue' />
     
    <table width="977" border="1" class="table1">
        <tr maxChecked="2">
            <td><input type="checkbox" value=31 ></td>
            <td><input type="checkbox" value=21></td>
            <td><input type="checkbox" value=15></td>
            <td><input type="checkbox" value=18></td>
            <td><input type="checkbox" value=17></td>
             
        </tr>
        <tr maxChecked="3">
            <td><input type="checkbox" value=14></td>
            <td><input type="checkbox" value=13></td>
            <td><input type="checkbox" value=12></td>
            <td><input type="checkbox" value=11></td>
            <td><input type="checkbox" value=10></td>
             
        </tr>
    </table>
     
    </body>
    </html>加了个取值的方法
      

  8.   


    我那个修改内容的时候,这个取值和限制的效果都没问题了,现在就是添加内容的时候那个限制选项的效果就是没有,我那个表格是当满足一定的条件才可以选择,就是有点绑定触发事件的那种,而添加时一开始默认不可选,像这样  然后点击某个按钮,才可以选择,但是这样的话,你写的那个jquery效果不起作用。
    不知道你明白我说的情况没有
      

  9.   


    一个项目里的代码  是mvc的  相互调用的,我不知道怎么贴啊
      

  10.   

    Quote: 引用 14 楼 wangwenjunyanda 的回复:

    Quote: 引用 13 楼 wangwei703 的回复:

    十分十分十分的感谢啊,我在csdn这么久,第一次遇到这么好的人啊,,这么认真的帮我解决了问题,现在心情比较激动,大恩不言谢了啊!!