开发者

Jquery function when a textbox loses focus

开发者 https://www.devze.com 2023-01-05 10:42 出处:网络
I have textbox that I want to run some jquery when the textbox loses focus, so after the user clicks out of the 开发者_Python百科text box.

I have textbox that I want to run some jquery when the textbox loses focus, so after the user clicks out of the 开发者_Python百科text box.

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.

How can I do this then?


focusout was added in v1.4. Three thoughts:

  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 noConflict mode and use jQuery instead of $.

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.

I did both an input and a textarea because I wasn't sure which you were using.


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

blur is the event that fires when an element loses focus. Check out 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 inside that element loses focus.


$("#idOfTextField").blur(function(){

  //your code

});


$(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消