将输入转换为制表符,使用隐藏字段

时间:2016-01-11 15:17:40

标签: javascript jquery

我有一个将“Enter”转换为“Tab”的脚本,它在我点击表单上的隐藏字段之前效果很好。

那么,有没有办法检测隐藏字段,仍然使用脚本(在IE& firefox中)?

<script type="text/javascript">
    function tabE(obj, e) {
        var e = (typeof event != 'undefined') ? window.event : e; // IE : Moz 
        if (e.keyCode == 13) {
            var ele = document.forms[0].elements;
            for (var i = 0; i < ele.length; i++) {
                var q = (i == ele.length - 1) ? 0 : i + 1; // if last element : if any other 
                if (obj == ele[i]) {
                    ele[q].focus();
                    break
                }
            }
            return false;
        }
    }
</script>

2 个答案:

答案 0 :(得分:1)

将您的代码更改为如下所示:

<script type="text/javascript">
    function tabE(obj, e) {
        var e = (typeof event != 'undefined') ? window.event : e; // IE : Moz 
        if (e.keyCode == 13) {
            var ele = document.forms[0].elements;
            for (var i = 0; i < ele.length; i++) {
                var q = (i == ele.length - 1) ? 0 : i + 1; // if last element : if any other 
                if (obj == ele[i] && $(ele[q]).is(":visible")) {
                    ele[q].focus();
                    break
                }
            }
            return false;
        }
    }
</script>

我刚添加了一张支票,以确保您要关注的元素可见(未隐藏)。

编辑:如果您想完全跳过隐藏字段,请使用以下代码。

function tabE(obj, e) {
  var e = (typeof event != 'undefined') ? window.event : e; // IE : Moz

  var self = $(obj),
    form = self.parents('form:eq(0)'),
    focusable, next;

  if (e.keyCode == 13) {
    focusable = form.find('input,a,select,button,textarea').filter(':visible');
    next = focusable.eq(focusable.index(obj) + 1);
    if (!next.length) {
      next = focusable.first();
    }
    next.focus();
    return false;
  }
}

小提琴:

https://jsfiddle.net/mwatz122/0zqzzmc1/

答案 1 :(得分:0)

我的提议是完整的js:

&#13;
&#13;
function tabE(obj, e) {
  var e = (typeof event != 'undefined') ? window.event : e; // IE : Moz
  if (e.keyCode == 13) {
    var ele = document.forms[0].elements;
    for (var i = 0; i < ele.length; i++) {
      var q = (i == ele.length - 1) ? 0 : i + 1; // if last element : if any other
      if (obj == ele[i]) {
        var style = window.getComputedStyle(ele[q]);
        while (style.display == 'none' || style.visibility != 'visible' || ele[q].type == 'hidden') {
          q = (q == ele.length - 1) ? 0 : q + 1; // if last element : if any other
          style = window.getComputedStyle(ele[q]);
        }
        ele[q].focus();
        break
      }
    }
    return false;
  }
}
window.onload = function() {
  var inp = document.getElementsByTagName("input")
  for(i=0; i<inp.length; i++) {
    inp[i].addEventListener("keyup", function(e) {
      tabE(this, e);
    });
  }
}
&#13;
<form>
    First name:<br>
    <input type="text" name="firstname">
    <br>
    Last name:<br>
    <input type="text" name="lastname">
    <input type="text" name="myhidden" style="visibility: hidden">
    <input type="hidden" name="myhidden1">
    <input type="text" name="myhidden2" style="display: none">
</form>
&#13;
&#13;
&#13;