2013-03-05 129 views
4

我有很少的输入字段需要更新。当按Tab键时,我需要将焦点移动到下一个字段,只有在对当前字段进行一些验证成功之后。如果失败,则保持在同一个字段中。防止在Firefox中默认的Tab键行为

function fieldFocus(e, nxFld){ 
    var key; 
    if (window.event) key = e.keyCode; 
    else if (e.which) key = e.which; 

    if (!e.shiftKey && key === 9) { 
    e.stopPropagation(); 
    e.preventDefault(); 

    // do validate {} 
    if (success) 
    $(nxFld).focus(); //set the focus to the next fld 
    else 
    // remain in the same field 
    } 
    return false; 
} 

$(currFld).bind("keydown",function(e) { 
    return fieldFocus(e, nxtFld); 
}); 

这在IE和Chrome中运行良好。但在Firefox中默认的焦点总是在验证之前触发。请帮助我,以防止Firefox的默认行为。

----编辑代码相关@Faizul哈桑的代码----

<script> 
    function fieldFocus(e, obj){ 
    var key; 
    if (window.event) key = e.keyCode; 
    else if (e.which) key = e.which; 

    if (!e.shiftKey && key === 9) { 
     // do validate 
     if (0 !== obj.value.length){ 
     var answer = confirm('Are you sure?') 
     if(answer) 
      return true; 
     else{ 
      // need to stop cursor focus to the next field 
      e.stopPropagation(); 
      e.preventDefault(); 
     } 
     } 
     else{ 
     e.stopPropagation(); 
     e.preventDefault(); 
     } 
    } 
    return false; 
    } 
</script> 

这是即时得到真正的问题,用户确认Firefox中的焦点移动到下一个前场。但在IE和Chrome中工作正常。

+0

要触发哪个事件的fieldFocus函数?是否可以用你的代码创建jsFiddle来触发该函数? – 2013-03-05 11:13:52

+0

@Faizul哈桑,我已经更新了代码示例..对不起,我不能更新它到jsFiddle。希望你能理解我的要求.. – idsTech 2013-03-05 11:36:46

+0

我不认为你必须通过下一个领域。只需尝试我已发布的答案。它工作正常...希望这会帮助你.. – 2013-03-05 11:41:19

回答

0

我发现这会导致问题火狐的东西很少锻炼后。这是'按键'事件。

当将preventdefault()应用于keydown但只在Firefox中时,'keypress'事件仍然会触发。

所以我处理了'按键'如下,并解决了我的问题。

希望这可以帮助很多人。

var browsFix = false; 
function fieldFocus(e, nxFld){ 
    var key; 
    if (window.event) key = e.keyCode; 
    else if (e.which) key = e.which; 

    if (!e.shiftKey && key === 9) { 
    browsFix = true; 
    e.stopPropagation(); 
    e.preventDefault(); 

    // do validate {} 
    if (success) 
     $(nxFld).focus(); //set the focus to the next fld 
    else 
    // remain in the same field 
    } 
    return false; 
} 

$(currFld).bind("keydown",function(e) { 
    browsFix = false; 
    return fieldFocus(e, nxtFld); 
}); 

$(currFld).bind("keypress",function(e) { 
    if (browsFix) 
    return false; 
}); 
1

尝试这样的事情。这也适用于Chrome和Firefox。

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8" /> 
    <script> 
     function fieldFocus(e, obj){ 
     var key; 
     if (window.event) key = e.keyCode; 
     else if (e.which) key = e.which; 

     if (!e.shiftKey && key === 9) { 
      // do validate 
      if (0 !== obj.value.length){ 
      return true; 
      } 
      else{ 
      e.stopPropagation(); 
      e.preventDefault(); 
      } 
     } 
     return false; 
     } 
    </script> 
</head> 
<body> 
    <input type="text" id="first-field" onkeydown="fieldFocus(event, this);" /> 
    <input type="text" id="second-field" onkeydown="fieldFocus(event, this);" /> 
    <input type="text" id="third-field" onkeydown="fieldFocus(event, this);" /> 
    <input type="text" id="fourth-field" onkeydown="fieldFocus(event, this);" /> 
    <input type="text" id="fifth-field" onkeydown="fieldFocus(event, this);" /> 
    <input type="text" id="sixth-field" onkeydown="fieldFocus(event, this);" /> 
</body> 

请注意,这是因为你火的功能是不是在你的代码中提到的方式您参考示例代码。您可以使用jQuery轻松调用​​事件的函数,而不是像onkeydown = functionName(<params>)那样为所有输入元素调用它。希望这会帮助你。

更新:相同的代码,但jQuery的集成

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8" /> 
    <script src="jquery-1.8.2.js"></script> 
    <script> 
     $(document).ready(function(){ 
     $('.input-element').each(function(index, value){ 
      $(value).keydown(function(event){ 
      fieldFocus(event, this); 
      }); 
     }); 

     function fieldFocus(e, obj){ 
      var key; 
      if (window.event) key = e.keyCode; 
      else if (e.which) key = e.which; 

      if (!e.shiftKey && key === 9) { 
      // do validate     
      if (0 !== obj.value.length){ 
       return true; 
      } 
      else{ 
       e.stopPropagation(); 
       e.preventDefault(); 
      } 
      } 
      return false; 
     } 
     }); 
    </script> 
    </head> 
    <body> 
     <input type="text" id="first-field" class="input-element" /> 
     <input type="text" id="second-field" class="input-element" /> 
     <input type="text" id="third-field" class="input-element" /> 
     <input type="text" id="fourth-field" class="input-element" /> 
     <input type="text" id="fifth-field" class="input-element" /> 
     <input type="text" id="sixth-field" class="input-element" /> 
    </body> 
</html> 
+0

谢谢你的回应。我确实尝试了你的代码,但没有得到我想要的。 在我的情况下,验证部分并不那么简单。有时它会通过一个对话框询问用户确认来验证。特别是在那个时候,我需要处理重点。检查我通过修改代码添加的代码。 – idsTech 2013-03-06 04:12:27