-2

我想为复选框列表 asp.net 控件编写 javascript 逻辑,如果复选框选择中的某些条件为真,则全选,否则取消全选。如何弄清楚这个检查取消选中并且使用javascript有价值?

4

2 回答 2

3

.checked将返回您是否选择,而不是您可以选择全部

例如,我举了一个示例

var idckbIsSolicitor = '<%= ckbIsSolicitor.ClientID %>';
var ckbIsSolicitor = document.getElementById(idckbIsSolicitor);

 if (ckbIsSolicitor.checked)
{
//select all
}
else
{
//deselect all
}
于 2012-05-04T09:02:46.150 回答
1
<div><input type="checkbox" class="checkall"> Check all</div>
    <div><input type="checkbox" class="check"> Checkbox</div>
    <div><input type="checkbox" class="check"> Checkbox</div>
    <div><input type="checkbox" class="check"> Checkbox</div>

    <select size="5" multiple="multiple" class="listbox">
      <option value="test1">test1</option>
      <option value="test1">test1</option>
      <option value="test1">test1</option>
      <option value="test1">test1</option>
      <option value="test1">test1</option>
    </select>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script>
$(function () { // this line makes sure this code runs on page load
    $('.checkall').click(function () {
        $('.check').attr('checked', this.checked); //if you want to select/deselect checkboxes use this
        $('.listbox option').attr('selected', this.checked);//if you want to select/deselect options in list use this
    });
});
</script>

http://jsfiddle.net/fMk2e/

于 2012-05-04T08:59:33.603 回答