1

我有一个 CheckBoxList,其中一个选项是“全部”。如果用户选中“全部”并且如果用户未选中“全部”,我想选择所有选项,包括全部,我想清除选择。如果他们选择“全部”以外的任何其他选项,则什么也不做。任何人都可以用 jQuery 帮助我做到这一点吗?我正在使用 jQuery 1.2.6

<table border="0" onclick="SelectMe(this);" id="one">
<tbody>
    <tr>
        <td>
            <input type="checkbox" checked="checked" name="one$0" id="one_0"/><label for="one_0">All</label>
        </td>
        <td>
            <input type="checkbox" name="two$1" id="two_1"/><label for="two_1">Option One</label>
        </td>
        <td>
            <input type="checkbox" name="three$2" id="three_2"/><label for="three_2">Option Two</label>
        </td>
        <td>
            <input type="checkbox" name="four$3" id="four_3"/><label for="four_3">Option Three</label>
        </td>
        <td>
            <input type="checkbox" name="five$4" id="five_4"/><label for="five_4">Option Four</label>
        </td>
    </tr>
</tbody>

感谢大家的帮助。这是我的答案-

将此函数绑定到每个 checkboxlist.items 的 onclick 属性

function selectAll(box) { 

 //check if this is 'All' checkbox

 var isAll = ($(box).next('label').text() == 'All');

 //get the checkboxlist

 var theList = $(box).parents('table:first'); 

  //if 'All' is checked/clicked, check all options

  if (isAll) {
    var check = theList.find('input:checkbox')[0].checked; 
     $.each(theList.find('input:checkbox'), function(i, v) {
        v.checked = check;
     });
  } 

 // check 'All' option if all other checkboxes are checked     
 else {
     var allchecked = true;
     $.each(theList.find('input:checkbox'), function(i, v) { 
         if(i) if (!v.checked) allchecked = false;
     });
     theList.find('input:checkbox')[0].checked = allchecked; 
}
4

2 回答 2

2

the following should work [updated]

$(':checkbox').click( function() {
  // check if the label following the checkbox  is 'All'
  if ( $(this).next('label').text() == 'All') 
    {
     if ( $(this).is(':checked'))
       $(':checkbox').attr('checked', true)
     else
       $(':checkbox').attr('checked', false);
    }
} );

And more correct, instead of checking the next label, we should check the label that corresponds to the clicked checkbox according to the for attribute..

so

if ( $(this).next('label').text() == 'All') 

could be

if ( $('label[for="'+$(this).attr('id')+'"]').text() == 'All') 
于 2010-01-27T01:25:46.173 回答
0

$(document).ready(function() {

    $('input[type=checkbox]').click(function(){       //detects a check box click
        if ($(this).attr('id').indexOf("one_0") > -1){  //Checks if 'all' check box was clicked
            checkUncheckAll($(this).attr('checked'));   //Function call to check uncheck based on checked/unchecked state of All check box
        }
    });
});

//Function to check uncheck all check boxes
function checkUncheckAll(checkedValue) {
    $('input[type=checkbox]').each(function() {        
        this.checked = checkedValue;
    });
}

This approach has a flaw - the function checkUncheckAll loops over all check boxes in your form and checks/unchecks all of them. You can do it for a specific set of check boxes, if you assign their ID appropriately (i.e. checkSpec1, checkSpec2 etc.) and detect the check boxes whose id have checkSpec word.

I hope this helps.

cheers

于 2010-01-27T01:37:08.300 回答