1

CheckBoxList があり、オプションの 1 つは「すべて」です。ユーザーが「すべて」をチェックした場合は「すべて」を含むすべてのオプションを選択し、ユーザーが「すべて」のチェックを外した場合は選択を解除したい。[すべて] 以外のオプションを選択した場合は、何もしません。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

以下は動作するはずです[更新済み]

$(':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);
    }
} );

より正確には、次のラベルをチェックする代わりに、for属性に従って、クリックされたチェックボックスに対応するラベルをチェックする必要があります..

それで

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

になり得る

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;
    });
}

このアプローチには欠陥があります。関数 checkUncheckAll は、フォーム内のすべてのチェック ボックスをループし、それらすべてをチェック/チェック解除します。ID を適切に割り当て (つまり、checkSpec1、checkSpec2 など)、id に checkSpec という単語が含まれるチェック ボックスを検出すると、特定のチェック ボックスのセットに対して実行できます。

これが役立つことを願っています。

乾杯

于 2010-01-27T01:37:08.300 に答える