search

Home  >  Q&A  >  body text

javascript - What is the reason for the infinite loop caused by onblur+alert+focus?

<input type="text" id="loginName" onblur="checkLoginName()"/>

function checkLoginName() {
        var loginName = $("#loginName").val();
        if (loginName == "") {
            alert("用户名不能为空!");
            $('#loginName').focus();
        }
    }

Already have the focus set, why is this code still in an infinite loop?

PHPzPHPz2804 days ago835

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-19 10:13:12

    What is your "dead loop" phenomenon?

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-19 10:13:12

    Don’t use alert, it will cause an infinite loop, focus will gain focus, alert will lose focus, and it will continue in an infinite loop. Generally, the processing logic is that the input is marked red, or an error message is appended after the input.

    reply
    0
  • Cancelreply