日期:2014-05-16  浏览次数:20339 次

js实现checkbox选中的问题
有两个table
每个table中有十个左右的checkbox,ID为checkbox1,2......
怎么用js实现每个table中只能选中三个checkbox
------解决方案--------------------
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<table id="tb1"><tr><td><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /></td></tr></table>
<table id="tb2"><tr><td><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /><input type="checkbox" /></td></tr></table>
<script>
    $('#tb1 :checkbox,#tb2 :checkbox').click(function () {
        var  tb = $(this).closest('table'), ckCount = tb.find(':checkbox:checked').size();
        if (ckCount > 3) this.checked = false;
    });
</script>