2015-04-01 71 views
0

当用户在输入字段1中输入一些“垃圾”时,blur事件触发一个函数来通知错误,然后触发错误的字段为select()。这在IE和Safari中运行良好,但Firefox和Chrome跳过select()。如果我使用FireBug来浏览代码,它可以按照设计工作。在Firefox和Chrome中选择方法失败

下面是代码样本:

function checkForJunk(fld) { 
 
    if (fld.value == 'junk') { 
 
    alert('please take out the junk'); 
 
    fld.select(); 
 
    } 
 
}
<form name="junk" action="junk.htm" method="post"> 
 
    Input1: 
 
    <input type="text" name="morejunk" value="" onBlur="checkForJunk(this);" /> 
 
    <br> 
 
    Input2: 
 
    <input type="text" name="evenMorejunk" value="" onBlur="checkForJunk(this);" /> 
 
</form>

+0

它工作正常,我对铬你有没有试过fld.focus()? – giordanolima 2015-04-01 16:25:31

+0

要么在模糊事件期间无法重新对焦元素,要么因为您正在聚焦另一个元素,所以该焦点事件发生在select()后面。如果将'select()'调用放在'setTimeout'中,它可以正常工作。 – 2015-04-01 16:32:21

回答

0

当我把选择的setTimeout的

setTimeout(function() { 
     fld.select(); 
}, 1);