簡體   English   中英

復選框更改事件未觸發

[英]Checkbox change event not firing

這是我的代碼:

<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>

當我點擊“全部檢查”超鏈接時,我希望為每個復選框觸發更改事件。 但是,這種情況並沒有發生。

有任何想法嗎?

非常感謝!

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

實例: http//jsfiddle.net/NRPSA/1/

更改屬性時,請使用以下內容:

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

或者像其他人建議的那樣在您的代碼中:

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

嘗試改變

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

至 -

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

這應該會強制觸發“改變”事件。

工作演示 - http://jsfiddle.net/ipr101/pwmBE/

試試這個。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM