javascript Action .on('blur') 对输入文本不起作用 - jQuery 1.9+

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20253072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:05:48  来源:igfitidea点击:

Action .on('blur') doesn't work on input text - jQuery 1.9+

javascriptjqueryhtml

提问by Matheus Gelinski

I updated my application with a newest jQuery Version (1.9.1). According the release update, the .live() method was discontinued and removed from 1.9+ jQuery versions. The new resource is now the .on(), with the same syntax.

我使用最新的 jQuery 版本 (1.9.1) 更新了我的应用程序。根据发布更新,.live() 方法已停止并从 1.9+ jQuery 版本中删除。新资源现在是 .on(),具有相同的语法。

Early, I've wrote the jQuery code below:

早些时候,我写了下面的 jQuery 代码:

jQuery("#addressTypes ul li input[type=text]").live('blur', function () {
    var item = {
        addressType: jQuery("#addressTypes ul input[type=text]").val(),
        idAddressType: jQuery("#addressTypes ul input[type=text]").parent().attr("id")
    }
    submitUpdateAddressType(item);
});

This code works on older versions, but stopped working with the jQuery update.

此代码适用于旧版本,但停止使用 jQuery 更新。

Can someone help me? Remembering that the item jQuery("#addressTypes ul li input[type=text]")cannot be changed due to business issues.

有人能帮我吗?请记住,jQuery("#addressTypes ul li input[type=text]")由于业务问题,该项目无法更改。

回答by Thanh Trung

jQuery(document).on('blur', "#addressTypes ul li input[type=text]", function () { });

回答by Jason P

In the case of static elements, you can simply replace the word livewith on.

在静态元素的情况下,你可以简单地替换的单词liveon

In the case of dynamically created elements, you instead need to use the version of on()that takes three parameters:

对于动态创建的元素,您需要使用on()带有三个参数的版本:

$(StaticContainer).on('event', 'selector', function() { });

See the section of the documentationabout direct and delegated events.

请参阅有关直接和委托事件的文档部分。

Simplest solution would be this:

最简单的解决方案是这样的:

jQuery(document).on('blur', "#addressTypes ul li input[type=text]", function () {

Though it's generally best to find a static container as close to the target elements as possible (instead of document)

虽然通常最好找到一个尽可能靠近目标元素的静态容器(而不是document