在较新版本中,jQuery .change() 事件是通过 .click() 触发的吗?
我目前正在升级我的应用程序以使用 jQuery 1.6.1(之前使用 1.4.4),发现现在 .click()
事件会自动触发 .change()
活动也是如此。
我在这里创建了一个简单的示例: http://jsfiddle.net/wDKPN/
请注意,如果您包含 1.4.4当触发 .click()
事件时,.change()
函数将不会触发。但切换到 1.6 时,.change()
事件会在触发 .click()
时触发。
两个问题:
这是一个错误吗?似乎以编程方式触发
.click()
不应该也触发其他事件(例如例如,自动触发.blur()
和.focus()
来帮助“模仿”用户的点击似乎是错误的。绑定
change()
事件然后触发两者点击的正确方法是什么() 和该元素的change()
事件? 我是否只需调用.click()
,并依赖于.change()
也会触发吗?$('#myelement').change(function() { // 做一些事情 }); $('#myelement').click(); // 单击和更改都会触发,耶!
在我的旧代码中,我使用此模式在 ajax 调用后初始化一些复选框(及其选中的状态和值):
$('#myelement').change(function() {
// do some stuff including ajax work
}).click().change();
但在 1.6.1 中,我的逻辑触发两次(一次为 .click()
一次用于 .change()
)。我可以依靠删除 .change()
触发器并希望 jQuery 在未来版本中继续以这种方式运行吗?
I'm currently upgrading my application to use jQuery 1.6.1 (previously using 1.4.4) and found that now the .click()
event automatically triggers a .change()
event as well.
I created a simple example here: http://jsfiddle.net/wDKPN/
Notice if you include 1.4.4 the .change()
function will not fire when the .click()
event is triggered. But when switching to 1.6, the .change()
event is fired when .click()
is triggered.
Two questions:
Is this a bug? It seems that programmatically triggering
.click()
shouldn't also fire other events (for example, it would seem wrong to also automatically fire.blur()
and.focus()
, to help "mimic" a user's click).What is the proper way for me to bind a
change()
event and then trigger both aclick()
andchange()
event for that element? Do I simply call.click()
, and rely on the fact that.change()
will also fire?$('#myelement').change(function() { // do some stuff }); $('#myelement').click(); // both click and change will fire, yay!
In my old code I'm using this pattern to initialize some checkboxes (and their checked states and values) after an ajax call:
$('#myelement').change(function() {
// do some stuff including ajax work
}).click().change();
But in 1.6.1 my logic fires twice (once for .click()
and once for .change()
). Can I rely on just removing the .change()
trigger and hope that jQuery continues to behave this way in future versions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好的方法是:
要执行初始化操作,您只需将自定义事件绑定到它,并在页面第一次加载时触发它。在任何版本中,没有人会夺走这一点。
而我相信
change
事件将继续存在于所有版本中,因为它已经存在了很长时间,而且这样效果很好。最后,这是一个幸福的结局,因为自定义事件
initCheckboxes
将在页面加载时仅触发一次,而change
事件将始终侦听并在更改状态时触发。Best way to do this is:
To do your initialization stuff you just bind to it a custom event, which you trigger it the first time the page loads. This, no one will take away from you on any versions.
Whereas
change
event, I believe, will continue to be there on all versions, because it has been for so long, and it just works nicely that way.In the end, this is a happy ending story, because the custom event
initCheckboxes
will fire just once on page load andchange
event will always listen and fire on change state.我想说这是 jQuery 1.4.4 中的一个错误。删除 jQuery 事件处理程序并使用标准
addEventListener
会产生与 jquery 1.6.1 相同的结果。http://jsfiddle.net/jruddell/wDKPN/26/
我也会使用
triggerHandler
专门调用 jQuery 事件处理程序。如果您希望事件模型确定要调用哪些处理程序,请使用click
、change
等。I would say this was a bug in jQuery 1.4.4. Removing the jQuery event handlers and using standard
addEventListener
produces the same result as jquery 1.6.1.http://jsfiddle.net/jruddell/wDKPN/26/
Also I would use
triggerHandler
to specifically invoke a jQuery event handler. If you want the event model to determine which handlers to call, then useclick
,change
etc.忘记复选框的单击事件。更改事件处理一切。
亲自查看:http://jsfiddle.net/zupa/UcwdT/
(此演示设置为 jQuery 1.8,但也适用于 1.6。)
Forget about the click event for checkboxes. The change event handles everything.
See for yourself: http://jsfiddle.net/zupa/UcwdT/
(This demo is set to jQuery 1.8 but works in 1.6 as well.)
我认为你可以通过将change()放在点击处理程序中然后触发点击来使其适用于jQuery 1.4.4和1.6实现。
看一下那里的简单示例。
I think you can make it work for both jQuery 1.4.4 and 1.6 implementations by putting change() within click handler and then triggering the click.
Have a look there for simple example.