在 jQuery 中将 live() 转换为 on()
我的应用程序动态添加了下拉菜单。用户可以根据需要添加任意数量。
我传统上使用 jQuery 的 live()
方法来检测这些下拉列表之一何时被 change()
ed:
$('select[name^="income_type_"]').live('change', function() {
alert($(this).val());
});
从 jQuery 1.7 开始,我已将其更新为:
$('select[name^="income_type_"]').on('change', function() {
alert($(this).val());
});
查看文档,这应该是完全有效的(对吗?) - 但事件处理程序永远不会触发。当然,我已经确认jQuery 1.7已加载并正在运行等。错误日志中没有错误。
我做错了什么?谢谢!
My application has dynamically added Dropdowns. The user can add as many as they need to.
I was traditionally using jQuery's live()
method to detect when one of these Dropdowns was change()
ed:
$('select[name^="income_type_"]').live('change', function() {
alert($(this).val());
});
As of jQuery 1.7, I've updated this to:
$('select[name^="income_type_"]').on('change', function() {
alert($(this).val());
});
Looking at the Docs, that should be perfectly valid (right?) - but the event handler never fires. Of course, I've confirmed jQuery 1.7 is loaded and running, etc. There are no errors in the error log.
What am I doing wrong? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
on
文档 指出(以粗体显示;)):相当于
.live()
类似于虽然将事件处理程序绑定到尽可能靠近元素(即层次结构中更接近的元素)会更好。
更新:在回答另一个问题时,我发现
.live文档
:
The
on
documentation states (in bold ;)):Equivalent to
.live()
would be something likeAlthough it is better if you bind the event handler as close as possible to the elements, that is, to an element being closer in the hierarchy.
Update: While answering another question, I found out that this is also mentioned in the
.live
documentation:除了所选答案之外,
在等待应用程序迁移时将
jQuery.live
移植到 jQuery 1.9+。将其添加到您的 JavaScript 文件中。注意: 由于
this.selector
已被删除,上述函数在 jQuery v3 中将不起作用。或者,您可以使用 https://github.com/jquery/jquery-migrate
In addition to the selected answer,
Port
jQuery.live
to jQuery 1.9+ while you wait for your application to migrate. Add this to your JavaScript file.Note: Above function will not work from jQuery v3 as
this.selector
is removed.Or, you can use https://github.com/jquery/jquery-migrate
刚刚找到了一个更好的解决方案,不涉及编辑第三方代码:
https://github.com/jquery/jquery- migrate/#readme
在 Visual Studio 中安装 jQuery Migrate NuGet 包,使所有版本控制问题消失。下次 Microsoft 更新其不显眼的 AJAX 和验证模块时,也许可以在不使用迁移脚本的情况下再次尝试,看看是否解决了问题。
由于 jQuery Migrate 由 jQuery 基金会维护,我认为这不仅是第三方库的最佳方法,也是为您自己的库获取详细说明如何更新它们的警告消息的最佳方法。
Just found a better solution which doesn't involve editing third party code:
https://github.com/jquery/jquery-migrate/#readme
Install the jQuery Migrate NuGet package in Visual Studio to make all the versioning issues go away. Next time Microsoft update their unobtrusive AJAX and validation modules perhaps try it without the migrate script again to see if they resolved the issue.
As jQuery Migrate is maintained by the jQuery Foundation I think this is not only the best approach for third party libraries and also to get warning messages for your own libraries detailing how to update them.
除了所选答案之外,
如果您使用 Visual Studio,则可以使用Regex Replace。
在编辑>查找和替换>在文件中替换
或 Ctrl + Shift + H
在“查找和替换”弹出窗口中,设置这些字段
查找内容:
\$\((.*)\)\.live\ ((.*),
替换为:
$(document.body).on($2,$1,
在查找选项中选中“使用正则表达式”
In addition to the selected answers,
If you use Visual Studio, you can use the Regex Replace.
In Edit > Find and Replace > Replace in Files
Or Ctrl + Shift + H
In Find and Replace pop-up, set these fields
Find what:
\$\((.*)\)\.live\((.*),
Replace with:
$(document.body).on($2,$1,
In find options check "Use Regular Expressions"
jquery version xxxjs 打开编辑器,找到
jQuery.fn.extend
添加代码
示例:jquery-2.1.1.js -->第 7469 行 (jQuery.fn.extend)
示例图像视图
jquery verision x.x.x.js open editor, find
jQuery.fn.extend
add code
Example : jquery-2.1.1.js --> line 7469 (jQuery.fn.extend)
Example images view