开发者

Checkbox change event not firing

开发者 https://www.devze.com 2023-04-04 19:07 出处:网络
This is my code: <html> <head> <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js\"></script>

This is my code:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<script>
  $(document).ready(function() {
    $('.chk').live('change', function() {
      alert('change event fired');
    });
    $('a').click(function() {
      $('.chk').attr('checked', true);
    });
  });
</script>
</head>
<body>
<div class="chkHolder">
  <input type="checkbox" class="chk" name="a" id="a" />
  <input type="checkbox" class="chk" name="b" id="b" />
</div>
<a href="#">check all</a>
</body>
</html>

When I click on the "check all" h开发者_开发问答yperlink, I want the change event fired for each of the checkboxes. However, that is not happening.

Any ideas?

Many thanks!


use: $('.chk').attr('checked', true).change();

Live example: http://jsfiddle.net/NRPSA/1/


When you change the attribute also use the following:

$('.chk').trigger('change');

Or in your code like other people have suggested:

$('.chk').attr('checked', true).trigger('change');


Try changing

$('a').click(function() {
      $('.chk').attr('checked', true);
});

To -

$('a').click(function() {
      $('.chk').attr('checked', true).trigger('change');
 });

That should force a trigger of the 'change' event.

Working demo - http://jsfiddle.net/ipr101/pwmBE/


try this.

<div id ="chkall"> <a href="#">check all</a> <div>

            $('#chkall a').live("change", function() {
            $('.chk').attr('checked', true);
            });
0

精彩评论

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