简体   繁体   English

输入字段模糊时验证字段,如果无效则取消模糊事件

[英]on blur of input field validate field and cancel blur event if not valid

I am validating a field on blur event. 我正在验证模糊事件的字段。 And if the field is invalid I want the focus to remain in same field and not go to next field. 如果该字段无效,我希望焦点保持在同一字段中,而不要转到下一个字段。 When I do $(this).focus() within my blur function it only works in Chrome and not in Firefox. 当我在我的模糊函数中执行$(this).focus() ,它仅适用于Chrome,不适用于Firefox。 I tried the settimeout function but the problem is that the focus goes to next field(#myInput2) and then comes back to field(#myInput1). 我尝试了settimeout函数,但问题是焦点转到下一个字段(#myInput2),然后又回到了字段(#myInput1)。 I dont want that as the next field also raises error cause it will be blurred again and it is empty. 我不希望因为下一个字段也引发错误,因为它将再次模糊并且为空。 Is there a way to solve this in Firefox?? 有没有办法解决这个问题?
My code : 我的代码:

$('#myInput1').blur(function () {
    if (!validField1($(this).val())) {
        alert("Invalid");
        $(this).focus(); // Does not work in FF 
    }
});


$('#myInput2').blur(function () {
    if (!validField2($(this).val())) {
        alert("Invalid");
        $(this).focus();
    }
});

I asked this question once, these are generally browser features which is not lock down a user to one input box. 我曾经问过这个问题,这些通常是浏览器功能,不能将用户锁定到一个输入框。 User should have choice of going away if they wanted, whether or not it is valid. 用户应该选择是否要离开,无论它是否有效。

So, Focus the same element after the blur event is disallowed. 因此,在模糊事件被禁止之后,将相同的元素聚焦。

Edit: 编辑:

It seems chrome browser allows such effect such behavior but firefox and internet explorer does not. Chrome浏览器似乎允许这种行为,但Firefox和Internet Explorer不允许。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM