开发者

jquery, checkbox select all not enabling the events on other checkboxes

开发者 https://www.devze.com 2023-01-29 06:27 出处:网络
I have a select all checkbox. <br>DK Filtering : <input type=\"checkbox\" name=\"asdf\" id=\"asdf\" value=\"A\" CHECKED>A

I have a select all checkbox.

<br>DK Filtering :
    <input type="checkbox" name="asdf" id="asdf" value="A" CHECKED>A
    <input type="checkbox" name="asdf" id="asdf" value="B">B
    <input type="checkbox" name="asdf" id="asdf" value="C_EVAL">CEval
    <input type="checkbox" nam开发者_Python百科e="asdf" id="asdf" value="D_EVAL">DEval
    <input type="checkbox" name="dkfilterall" id="dkfilterall" value="YES">Select All
<br>
    <script>
    \$("input[name=dkfilterall]").click(function() {
       if (\$('input[name=dkfilterall]').is(':checked')== true)
       {
            \$('input[name=asdf]').attr('checked', true);
       }
       if (\$('input[name=dkfilterall]').is(':checked')== false)
       {
            \$('input[name=asdf]').attr('checked', false);
       }
    });            
    </script>

Somewhere later, I have this function:

<script>
        \$("input[name=asdf]").live(function() {

...
</script>

I thought the act of checking the individual checkboxes through select all checkbox would trigger the function of the individual checkboxes but this isn't happening.

What do I need to do to correct this?

thanks Gordon


After you check the boxes, you need to call their corresponding .change() function to trigger them:

$('input[name=asdf]').attr('checked', true).change();

Here's a (simplified) working example that checks all the boxes and then calls their change() function.


You have to specify an event type for live, so .live('click', function() {});


Because you are checking if the checkboxes are already checked, you can just call the click event on them and it will trigger the functions. i.e.

$("input[name=dkfilterall]").click(function() {
       if ($('input[name=dkfilterall]').is(':checked')== true)
       {
            $('input[name=asdf]').click();
       }
       if ($('input[name=dkfilterall]').is(':checked')== false)
       {
            $('input[name=asdf]').click();
       }
    });     


  1. If you want to trigger some event (click, change) after you manually checked a checkbox, you must call trigger method to the object:

    $('input[name=asdf]').attr('checked', true)
       .trigger("change"); // <-- this code added
    
  2. In your example you trying to use live method that used to bind a trigger for static and dynamically created elements. To use it correctly the code must be like this:

    $("input[name=asdf]").live("click", function() {
       // Live handler called.
    });
    

See more information on official jquery site.

0

精彩评论

暂无评论...
验证码 换一张
取 消