文本框失去焦点时的 Jquery 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3151330/
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
Jquery function when a textbox loses focus
提问by twal
I have textbox that I want to run some jquery when the textbox loses focus, so after the user clicks out of the text box.
我有一个文本框,当文本框失去焦点时,我想运行一些 jquery,所以在用户单击文本框之后。
I have tried to do this
我试过这样做
$("#textbox").focusout(function () {
alert("hello");
});
but I get an error saying Object doesn't support this property or method.
但我收到一条错误消息,说 Object 不支持此属性或方法。
How can I do this then?
那我该怎么做呢?
采纳答案by T.J. Crowder
focusout
was added in v1.4. Three thoughts:
focusout
在 v1.4 中添加。三个想法:
- Could you be using an earlier version of jQuery?
- Does your field really have the id
textbox
? - Are you also using Prototype or MooTools (or anything else that might be taking over
$
)? If so, use jQuery'snoConflict
mode and usejQuery
instead of$
.
- 您可以使用早期版本的 jQuery 吗?
- 你的领域真的有 id
textbox
吗? - 您是否也在使用 Prototype 或 MooTools(或其他任何可能接管的东西
$
)?如果是这样,使用jQuery的noConflict
模式,并使用jQuery
替代$
。
Other than that, it should (does) work.
除此之外,它应该(确实)工作。
Here's an example (using an alert as you did): http://jsfiddle.net/QzmZp/1/
and another not using an alert (because that freaked IE7 out): http://jsfiddle.net/QzmZp/2/
Someone earlier asked about browser versions, I've tried the above with Chrome 5, IE6, IE7, and FF3.6; all fine.
这是一个例子(像你一样使用警报):http: //jsfiddle.net/QzmZp/1/
和另一个不使用警报(因为那吓坏了 IE7):http: //jsfiddle.net/QzmZp/2/
之前有人问过浏览器版本,我在Chrome 5、IE6、IE7和FF3.6上都试过上面的;一切都很好。
I did both an input
and a textarea
because I wasn't sure which you were using.
我做了一个input
和一个,textarea
因为我不确定你在使用哪个。
回答by Vivin Paliath
jQuery("#textbox").blur(function() {
alert("hello");
});
blur
is the event that fires when an element loses focus. Check out jQuery.blur
.
blur
是元素失去焦点时触发的事件。退房jQuery.blur
。
EDIT
编辑
Not sure if this is what you want, but if you are really trying to use focusout
check out T. J. Crowder's solution. For your situation though, the blur
event might be want you need since you want to detect the loss-of-focus on the textbox itself. focusout
fires when an element or any element insidethat element loses focus.
不确定这是否是您想要的,但如果您真的想使用,focusout
请查看TJ Crowder 的解决方案。但是,对于您的情况,该blur
事件可能是您需要的,因为您想检测文本框本身的焦点丢失。focusout
当一个元素或该元素内的任何元素失去焦点时触发。
回答by surthi deepak
$("#idOfTextField").blur(function(){
//your code
});
回答by Bachask8
$(document).ready(function () {
////////////////////////ALL textbox to upper
$("input[type=text]").blur(function () {
$(this).val($(this).val().toUpperCase());
});
});
To convert all textbox to upper when they lose focus.
在失去焦点时将所有文本框转换为上。