javascript - 防止 Firefox 中的默认 Tab 键行为

标签 javascript jquery firefox focus

我要更新的输入字段很少。当按 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 Hasan 的代码相关的编辑代码----

<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 中它工作正常。

最佳答案

尝试这样的事情。这在 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 轻松调用 keydown 的函数。事件而不是为所有输入元素调用它,如 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>

关于javascript - 防止 Firefox 中的默认 Tab 键行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15222105/

相关文章:

jquery - 改变 Accordion 行为

java - Linux (Ubuntu) Java 插件和控制台

javascript - 是否可以让我的 firefox 扩展程序获得对其他程序的关注?

javascript - 如何从JS更改输入类型日期的最大值或最小值

javascript - Jquery 工具提示验证文件

javascript - 是否可以在不完全禁用 JavaScript 的情况下禁用 AJAX?

javascript - 为什么 parseInt() 在 Firefox 中比 *1 慢这么多?

javascript - 从 ES6 类过滤属性

javascript - Select2 预选 val 数组仅解析数组的第一个值

javascript - 浏览器窗口之间是否可以进行基于事件的通信?