简体   繁体   中英

jQuery blur event firing on load, instead of when expected

I'm working on a project which will require some form validation, which I'm using jquery for. There's a field where a user will enter their email, and once they have filled out that field I want to check it.

Currently, the first part of my JavaScript looks like this:

$(window).load(function()
{
    var email = $("#registerEmail");
    email.blur(alert("stuff")); //will call a validation function

Right now, I get the "stuff" alert as soon as the page loads. My understanding was that blur would only fire once an element gained focus and then lost it -- am I misunderstanding this? Shouldn't this alert only execute once a user clicks or types in the email form and then clicks or types somewhere else, rather than immediately when the page loads?

You are actually executing the alert function when you do it that way. You need to provide a function that can be called later. Do

email.blur(function () {
    alert("stuff");
});
$(document).ready(function(){
    $("#registerEmail").blur(function(){
            //your alert here
     });

});

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