开发者

jquery focus not working on blur

开发者 https://www.devze.com 2023-02-13 17:26 出处:网络
I am using jquery to keep the focus on a text box on blur if validation fails. But Its working in IE but not working FF. Any suggestions?

I am using jquery to keep the focus on a text box on blur if validation fails. But Its working in IE but not working FF. Any suggestions?

$("#inputBoxId").blur(function () {
   if ($(this).val() < 10) 
      $("#i开发者_JAVA技巧nputBoxId").focus();

});


Looks like you need to use setTimeout according to this question. Since you are giving focus to an element immediately you need to account for some time for the element to go out of focus first.

$("#inputBoxId").blur(function() {
    if ($(this).val() < 10) {
        setTimeout(function() {
            $("#inputBoxId").focus();
        }, 100);
    }
});

Example of it working on jsfiddle, tested out on chrome dev channel, firefox, and IE8.


val() will return string, not number. Try converting and it should work:

var value = parseInt($(this).val(), 10);
if (isNaN(value) || value < 10) 
      $("#inputBoxId").focus();

This will also deal with non numeric values.


$(this).val().length will also give the length

$("#inputBoxId").blur(function () {
    if($(this).val().length < 10 ){
          $(this).focus();
     }
});


// Page visibility :

    document.addEventListener('visibilitychange', function () {

        if (document.hidden) {
            // stop running expensive task

        } else {
            // page has focus, begin running task

        }
    });
0

精彩评论

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

关注公众号