简体   繁体   English

使用Enter键作为JavaScript中的标签时,如何跳过隐藏字段

[英]How do you skip over hidden fields when using enter key as a tab in javascript

I have a asp.net web application that is being developed for IE11 and I am trying to have the Enter key function the same as the Tab key. 我有一个正在为IE11开发的asp.net Web应用程序,我试图使Enter键功能与Tab键相同。 Most of the time it works fine with the code below. 在大多数情况下,它可以与下面的代码配合使用。 However if there are fields that are disabled or in a div that is hidden the Enter key does not advance to the next enabled and visible field. 但是,如果存在禁用的字段或位于div中的字段,则Enter键不会前进到下一个启用的可见字段。

Here is what i have: 这是我所拥有的:

I have the following on my main div so it applies to all text boxes: onkeydown="changeEnterToTab() 我的主div上有以下内容,因此它适用于所有文本框:onkeydown =“ changeEnterToTab()

The functions fire properly but the getNextElement function returns a field that is not displayed or is disabled. 该函数正常启动,但是getNextElement函数返回一个未显示或被禁用的字段。

Any ideas? 有任何想法吗?

    function changeEnterToTab() {
        var node = (event.target) ? event.target : ((event.srcElement) ? event.srcElement : null);
        if ((event.keyCode == 13) && ((node.type == "text") || (node.type == "radio"))) {
            getNextElements(node).focus();
            return false;
        }
    }

    function getNextElement(field) {
        var form = field.form;
        for (var e = 0; e < form.elements.length; e++) {
            if (field == form.elements[e]) {
                e++;
                break;
            }
        }
        e++;
        debugger;
        while (form.elements[e % form.elements.length].type == "hidden") {
            e++;
        }
        return form.elements[e % form.elements.length];
    }

Put all your hidden controls at the top of the form so the user will only "tab" to the rest visible control. 将所有隐藏的控件放在表单顶部,以便用户仅“跳到”其余可见控件。

You can exclude the control from the tab order by set its tabIndex to -1 您可以通过将控件的 tabIndex设置为 -1将控件从标签顺序中排除

 
 
 
  
  <input type="text" name="username" tabIndex="-1" />
 
  

I did not find an actual answer to this question but I got around the issue by reordering the TabIndex completely to handle the instances where the controls are hidden/disabled. 我没有找到这个问题的实际答案,但是通过完全重新排序TabIndex来处理隐藏/禁用控件的实例,解决了这个问题。

Thanks for the ideas though guys. 谢谢你们的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM