简体   繁体   中英

jQuery .blur() not being affected - why?

I googled this question and couldn't find an answer.

I am trying to have an alert appear when a user unfocuses off of any input on my form. I discovered the blur and focusout function so my code looks like:

$("input").blur(function() {
    alert("Unfocused");
});

This works for my input fields no problem. But for my radio button inputs it doesn't work. I looked through the docs and google and couldn't figure out why.

$("input:radio").blur(function() {
    alert("Unfocused");
});

Test this code.

替代文字

Test with text and radio..

替代文字

THIS DOESN'T WORK IN SAFARI AND CHROME BECAUSE THEY ARE WEBKIT BASED.

Just put : before like this:

$(":input").blur(function() {
    alert("Unfocused");
});

The :input filter selector selects all type of inputs.

From Docs:

Description: Selects all input, textarea, select and button elements.

http://api.jquery.com/input-selector/


To select radio only, you should use :radio filter selector instead:

$(":radio").blur(function() {
    alert("Unfocused");
});

To get little speed gain, you should prepend keyword input like this:

$("input:radio").blur(function() {
    alert("Unfocused");
});

More Info:

http://api.jquery.com/radio-selector/


Here are more form selectors:

Form Selectors

There are a few selectors for form elements:

* :input Selects all form elements (input, select, textarea, button).
* :text Selects all text fields (type="text").
* :password Selects all password fields (type="password").
* :radio Selects all radio fields (type="radio").
* :checkbox Selects all checkbox fields (type="checkbox").
* :submit Selects all submit buttons (type="submit").
* :image Selects all form images (type="image").
* :reset Selects all reset buttons (type="reset").
* :button Selects all other buttons (type="button").
* :file Selects all <input type="file">. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM