需要复选框更改事件以响应以编程方式完成的已检查状态的更改 [英] Need checkbox change event to respond to change of checked state done programmatically

查看:95
本文介绍了需要复选框更改事件以响应以编程方式完成的已检查状态的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(jQuery 1.4.4)我有一个复选框,它带有一个.change()侦听器,该监听器对于用户单击该复选框时效果很好,但是现在我还需要在以编程方式更改jQuery中的复选框时使用它来触发.attr('checked','checked').我很高兴使用其他方法来完成这项工作.谢谢.

(jQuery 1.4.4) I have a checkbox that has a .change() listener that works fine for when the user clicks the checkbox, but now I also need it to fire when I programmatically change the checkbox in jQuery, using .attr('checked', 'checked'). I'm perfectly happy to use other methods to make this work. Thanks.

$('#foo').attr('checked', 'checked'); // programmatically change the checkbox to checked, this checks the box alright

$('#foo').change( function() {
  // this works when user checks the box but not when the above code runs
}

推荐答案

使用jquery click()更改复选框的状态,而不是更改其属性.

Use jquery click() to change the state of the checkbox rather than changing its attribute.

没有所有标记,我不确定这将是多么合适,但是最近我使用了这种方法以取得良好的效果.

Not having all your markup I'm not sure how suitable this would be, but I've used this method recently to good effect.

这篇关于需要复选框更改事件以响应以编程方式完成的已检查状态的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆