jQuery focus()和select()不适用于Firefox,但适用于Chrome



我正在尝试验证一个输入语句,如果输入错误,用户会收到一个Bubblediv,通知输入不正确。我试图将焦点恢复到错误的输入元素,并选择其中的文本。该代码适用于Chrome,但不适用于Firefox。有人能帮我吗。我在这里张贴代码:

$("#inner-tbl").on('blur', 'input[type=text]', function(e) {
    var checkval = $(this).val();
    checkval = checkval.replace(/s/g,'');
    $(this).val(checkval);
    console.log(checkval);
    var numericReg = /^d*[0-9](|.d*[0-9]|,d*[0-9])?$/;
    if(!numericReg.test(checkval)) {
        $(this).addClass("error");
        $(this).focus().select();
        console.log("Not an integer");
        var ip_width, ip_height, x;
        ip_width = $(this).outerWidth();
        ip_height = $(this).outerHeight();
        x = $(this).offset();
        console.log("Width :"+ip_width+" Height :"+ip_height+" Top :"+x.top+" Left :"+x.left);
        $("body").append("<div class='error_bubble'><span class='errordiv'>Numeric value.</span></div>");
        $(".error_bubble").css({
            position: 'absolute',
            top: (x.top+30)+'px',
            left: (x.left-30)+'px'
        });
    } 
});

这是jsfiddle:http://jsfiddle.net/neoragex/Wk35w/

在Chrome中;IE,文本输入字段被聚焦并且高亮

在Firefox中,文本输入字段是聚焦的,但光标位于未高亮显示的文本的开头

$("#surname").focus();
$("#surname").select(); // for Firefox

您可以使用return false及其对firefox的作用。

        $(this).focus().select();
        return false;

最新更新