简体   繁体   中英

jquery textbox focus event on infinity loop

i am using jquery to validate textbox value.

I have 2 textbox, txt1 & txt2. now, i wrote a jquery function.

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

            if ($("#txt1").val() == "") {
                $("#scarriername").show();
                $("#txt1").focus();
            }
            else {
                $("#scarriername").hide();
            }

        });

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

            if ($("#txt2").val() == "") {
                $("#sscaccode").show();
                $("#txt2").focus();
            }
            else {
                $("#sscaccode").hide();
            }
        });

Now, issue is. when i run the project. my position is on txt1 and when u use Tab to go txt2 with null or blank value. Focus event fire for both one & browser become hang due to infinite loop of FOCUS .

so, how can i handle it?

You should insert a setTimeout in order to set the focus after the blur event.

Second, you should insert a semaphore in order to avoid a loop (see code and comments):

var status = "valid"; // semaphore

$("#txt1").blur(function (e) {

    // if we are in programmatically focus, ignore this handler
    if(status == "invalid") 
        return ;

     if ($("#txt1").val() == "") {
         $("#scarriername").show();

         // set semaphore
         status = "invalid";

         // use setTimeout in order to set focus in the right moment
         setTimeout(function() {$("#txt1").focus(); status = "valid"},0);
     }
     else {
         $("#scarriername").hide();
     }

 });

// same as txt1
$("#txt2").blur(function () {
    if(status == "invalid") 
        return ;
    if ($("#txt2").val() == "") {
        $("#sscaccode").show();
        setTimeout(function() {$("#txt2").focus(); status = "valid"},0);

    }
    else {
        $("#sscaccode").hide();
    }
});

DEMO http://jsfiddle.net/SszUf/

try this

<input type="text" id="txt1" />
<input type="text" id="txt2" />

$("#txt2").focus(function () {
 if ($("#txt1").val() == "") {
        $("#scarriername").show();
        $("#txt1").focus();
    } 
    //alert(1);    
});

hope this help you

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

            if ($("#txt1").val() == "") {
                $("#scarriername").show();
                if ($("input:focus").length == 0)
                $("#txt1").focus();
            }
            else {
                $("#scarriername").hide();
            }

        });

Just add a line can solve this problem

By the way, the #scarriername should not be something like popup window, which will trigger other blur events

You can test the file below:

<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>

<input id="txt1"><input id="txt2"><input id="txt3"><input id="txt4">
<hr>
<h1 id="h1"></h1>
</body>
<script type="text/javascript">
$(function(){
$("input").blur(function () {
 if ($(this).val() == "") {
        document.getElementById("h1").innerHTML += "123";
        $(this).focus();
    }     
});});
</script>
</html>

Apparently when you hit the tab button you trigger blur event for both text boxes. With your code when txt1 gets blurred and has no content you get the focus on txt1, but when you do that you also trigger the blur event for txt2 and since it does not have any text in it you get the focus back to txt2. This keeps on going and going, focusing on txt1 and then txt2 and then txt1 and then txt2... You could put a simple if check on the second blur event handler to see if txt1 is still empty, and if so keep the focus on txt1 not allowing the client to pass to txt2:

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

            if ($("#txt1").val() == "") {
                $("#scarriername").show();
                $("#txt1").focus();
            }
            else {
                $("#scarriername").hide();
            }

        });

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

            if ($("#txt2").val() == "" && $("#txt1").val() != "") {
                $("#sscaccode").show();
                $("#txt2").focus();
            }
            else {
                $("#sscaccode").hide();
            }
        });

This is also one of the approach to solve your problem on pressing tab key.

$("#txt1").bind('keydown',function(e)
{
    if(e.keyCode == 9)
    {
        if ($("#txt1").val() == "") {
            $("#scarriername").show();
            return false;                
        }
        else {
            $("#scarriername").hide();
        }
    }
});
$("#txt2").bind('keydown',function(e)
{
    if(e.keyCode == 9)
    {
        if ($("#txt2").val() == "") {
            $("#sscaccode").show();
            return false;
        }
        else {
            $("#sscaccode").hide();
        }
    }
});

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