IE中的JQuery focusOut问题

时间:2013-08-03 05:17:55

标签: javascript jquery

我在IE中遇到focusout函数的小问题。

我有两个具有相同类的字段,我在jQuery中为focusout的该类编写了一个空的验证代码。

当我从一个空的字段中聚焦时,它会显示警告并聚焦到同一个字段。

在做这个焦点时,它会一次又一次地向我显示同一级别的警告。

怎么办?

JS:

$(".emptyValidate").focusout(function() {    
var currFocusOut = $(this).attr("inText");
    if($(this).val() == ""){            
      alert(currFocusOut+" should not be Empty");
      document.getElementById(currFocusOut).focus();
    }
});

加价:

<input type="text" inText="Name" id="Name" class="emptyValidate "/>
<input type="text" inText="Phone" id="Phone" class="emptyValidate "/>

3 个答案:

答案 0 :(得分:1)

Working Demo

$(".emptyValidate").focusout(function () {
    var currFocusOut = $(this).attr("id");
    if ($(this).val() == "") {
        alert(currFocusOut + " should not be Empty");
        $('#'+currFocusOut).focus();
    }
});

答案 1 :(得分:0)

尝试使用blur function之类的,

$(".emptyValidate").blur(function() {    
    var currFocusOut = $(this).attr("inText");
    if($(this).val() == ""){            
      alert(currFocusOut+" should not be Empty");
      document.getElementById(currFocusOut).focus();
    }
});

阅读blur()

或者,自定义属性inText可能无效,因此您可以在data中使用jquery

<input type="text" data-inText="Name" id="Name" class="emptyValidate "/>
<input type="text" data-inText="Phone" id="Phone" class="emptyValidate "/>

$(".emptyValidate").focusout(function() {  // use blur if not works  
    var currFocusOut = $(this).data("inText");
    if($(this).val() == ""){            
      alert(currFocusOut+" should not be Empty");
      document.getElementById(currFocusOut).focus();
    }
});

阅读data()

答案 2 :(得分:0)

尝试这样的内联函数:

<input type="text" inText="Name" id="Name" class="emptyValidate"  onfocusout="myFunction(this)"/>
<input type="text" inText="Phone" id="Phone" class="emptyValidate"  onfocusout="myFunction(this)"/>
function myFunction(el){
    var currFocusOut = $(el).attr("inText");
    if($(el).val() == ""){            
      alert(currFocusOut+" should not be Empty");
      document.getElementById(currFocusOut).focus();
    }
}