文本框失去焦点时的 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:56:04  来源:igfitidea点击:

Jquery function when a textbox loses focus

jquery

提问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

focusoutwas added in v1.4. Three thoughts:

focusout在 v1.4 中添加。三个想法:

  1. Could you be using an earlier version of jQuery?
  2. Does your field really have the id textbox?
  3. Are you also using Prototype or MooTools (or anything else that might be taking over $)? If so, use jQuery's noConflictmode and use jQueryinstead of $.
  1. 您可以使用早期版本的 jQuery 吗?
  2. 你的领域真的有 idtextbox吗?
  3. 您是否也在使用 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 inputand a textareabecause I wasn't sure which you were using.

我做了一个input和一个,textarea因为我不确定你在使用哪个。

回答by Vivin Paliath

jQuery("#textbox").blur(function() {
  alert("hello");
});

bluris 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 focusoutcheck out T. J. Crowder's solution. For your situation though, the blurevent might be want you need since you want to detect the loss-of-focus on the textbox itself. focusoutfires 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.

在失去焦点时将所有文本框转换为上。