复选框更改事件未触发

这是我的代码:

    $(document).ready(function() { $('.chk').live('change', function() { alert('change event fired'); }); $('a').click(function() { $('.chk').attr('checked', true); }); });    
check all

当我点击“全部检查”超链接时,我希望为每个复选框触发更改事件。 但是,这种情况并没有发生。

有任何想法吗?

非常感谢!

使用: $('.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/

试试这个。

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