简体   繁体   English

AutoTab javascript函数问题

[英]AutoTab javascript function problem

we've run into an issue with this while using it in our asp.net application. 在asp.net应用程序中使用它时,我们遇到了一个问题。

The purpose of it is as follows: 其目的如下:

  • When in a textbox -> tab once MaxLength is reached 在文本框->选项卡中时,达到MaxLength
  • When in a checkbox -> tab once the control is toggled with the keyboard (spacebar) 在复选框->选项卡中时,使用键盘(空格键)切换控件

Asides from buttons, those are the only controls on the form. 除了按钮之外,这些是表单上的唯一控件。 The function is toggled by the end user (saved by using a cookie). 该功能由最终用户切换(通过使用cookie保存)。

Issues: 问题:

  • As you'll notice, it goes about the process fairly randomly. 您会注意到,该过程相当随机。 Sometimes it performs quickly, sometimes it doesn't. 有时它执行得很快,有时却没有。 The way we have the form set presently is with a very large tab index range that will more than likely remain. 目前,我们设置表单的方式是使用很大的Tab索引范围,而且很可能会保留下来。 Not really worried about this issue to be honest, but nice if someone knows what to do. 老实说,我并不真的担心这个问题,但是如果有人知道该怎么做,那就太好了。

  • Checkbox functionality is working very oddly. 复选框功能运行异常。 Say you have CheckBoxA and CheckBoxB. 假设您有CheckBoxA和CheckBoxB。 When you use the keyboard to toggle CheckBoxA, it grays out the box as though it were disabled and sets focus to CheckBoxB. 当您使用键盘切换CheckBoxA时,它会变成灰色框(好像已被禁用),并将焦点设置为CheckBoxB。 At this point if you use the mouse and click anywhere on the document, it toggles CheckBoxA every time you click and ignoring normal mouse functionality for that area until you right click and then cancel the context menu. 此时,如果您使用鼠标并单击文档上的任意位置,则每次单击都会切换CheckBoxA,而忽略该区域的常规鼠标功能,直到右键单击并取消上下文菜单。 If you toggle CheckBoxB after it gains focus from AutoTab with the keyboard, CheckBoxA loses the disabled gray appearance (though it has never toggled) and the cycle repeats with CheckBoxB being the one with issues. 如果您在用键盘从AutoTab中获得焦点之后切换CheckBoxB,则CheckBoxA会失去禁用的灰色外观(尽管它从未切换过),并且循环重复,而CheckBoxB就是有问题的那个。

Here is the code important to the function. 这是对该功能重要的代码。 CheckCookie() is a check to make sure that the user has AutoTab set otherwise AutoTab doesn't happen. CheckCookie()是一项检查,以确保用户设置了AutoTab,否则不会发生AutoTab。 Array.contains goes through the array to find if the keycode entered is present in it. Array.contains遍历数组以查找是否存在输入的键控代码。

function test(input, inputClientID, e) {
if (checkCookie()) {
    var acceptedChars = ["48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "186", "187", "188", "189", "190", "191", "192", "219", "220", "221", "222"];
    if (input.type == "text") {
        len = document.getElementById(inputClientID).getAttribute("MaxLength");
        curLen = document.getElementById(inputClientID).getAttribute("Value");
        if ((len != null) && (len != "") && (curLen != null) && (curLen != "")) {
            if (len <= curLen.length) {
                var keycode;
                if (window.event) keycode = window.event.keyCode;


                if (e.shiftKey == true && keycode == 9) {
                    return;
                }
                else if (acceptedChars.contains(keycode)) { //(e.shiftKey == false && keycode != 9)
                    tabNextCtrl(input);
                }
            }
        }
    }
    else if (input.type == null) {
        if (input.firstChild.type == "checkbox") {
            var keycode;
            if (window.event) keycode = window.event.keyCode;

            if (keycode == 32) {

                //if (input.firstChild.checked) {
                //    input.firstChild.checked = false;
                //}
                //else {
                //    input.firstChild.checked = true;
                //}
                tabNextCtrl(input.firstChild);                    
            }
        }
    }
}}

The tabbing is done using a sort here (and where the bottleneck of issue #2 comes in I believe): 在这里使用一种排序方式来完成分页(我相信问题2的瓶颈在哪里出现):

function tabNextCtrl(input) {
var tbIndex = input;
var aIndex = 99999999;

for (i = 0; i < input.form.elements.length; i++) {
    if ((input.form.elements[i].tabIndex > input.tabIndex) && (input.form.elements[i].tabIndex < aIndex)) {
        aIndex = input.form.elements[i].tabIndex;
        tbIndex = input.form.elements[i];
    }
}
tbIndex.focus();
}

I apologize as this is very lengthy (the issue isn't something I know of having a common name, though I've seen the behavior before), but any assistance you can give with this issue would be very appreciated. 我很抱歉,因为这太长了(虽然我以前已经看到过这种行为,但我不知道这个问题有一个通用的名字),但是对于此问题您能提供的任何帮助将不胜感激。

Thanks, 谢谢,

After some modification, this is the solution that ended up working for what we needed. 经过一些修改,这就是最终可以满足我们需要的解决方案。 The tabindex aspect of it is quite haphazard, but we got it to work. 它的tabindex方面很杂乱无章,但我们使它起作用了。

JavaScript: JavaScript:

function Tab(currentField, e) {
    if (!(e.keyCode == 9 & e.shiftKey)) {
        if (currentField.value.length == currentField.maxLength) {
            if (e.keyCode != 16 & e.keyCode != 9) {
                tabNextCtrl(currentField);
            }
        }

        if (currentField.type == "checkbox") {
            tabNextCtrl(currentField);
        }
    }
}

function tabNextCtrl(input) {
    var tbIndex = input; 
    var aIndex = 99999999;
    for (i = 0; i < input.form.elements.length; i++) {
        if ((input.form.elements[i].tabIndex > input.tabIndex) && 
            (input.form.elements[i].tabIndex < aIndex)) {
            aIndex = input.form.elements[i].tabIndex; 
            tbIndex = input.form.elements[i];
        }
    }
    tbIndex.focus(); 
} 

CodeBehind (demo): CodeBehind(演示):

protected void Page_Load(object sender, EventArgs e)
    {
        this.CheckBox1.Attributes["onclick"] = "Tab(this, event)";
        this.CheckBox2.Attributes["onclick"] = "Tab(this, event)";
        this.TextBox1.Attributes["onkeyup"] = "Tab(this, event)";
    }

ASPX (demo): ASPX(演示):

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script language="javascript" src="Scripts/autotab.js" type="text/javascript"></script>
<asp:CheckBox ID="CheckBox1" runat="server" TabIndex="1" Text="First" />
<br />
<asp:CheckBox ID="CheckBox2" runat="server" TabIndex="2" Text="Second" />
<br />
<asp:TextBox ID="TextBox1" runat="server" TabIndex="3" MaxLength="4" Width="4em" />
<br />
<asp:CheckBox ID="CheckBox3" runat="server" TabIndex="4" Text="Last" />

 function Tab(currentField, e) {

    if (!(e.keyCode == 8 || e.keyCode == 37) ) {
        if (currentField.value.length == currentField.maxLength) {   
            $(currentField).next('input').focus();
        }
    }
    else{

        if (currentField.value.length == 0) {   
            $(currentField).prev('input').focus();

        }

    }

  }

<input name="phone" id="phone" class="input20p" onkeyup='Tab(this,event)'  maxlength="3"  type="text" border="0">
<input  maxlength="3" class="input20p" name="phone1"   type="text" border="0"   onkeyup='Tab(this,event)'>
<input class="input20p" name="phone2" maxlength="4"   type="text" border="0" onkeyup='Tab(this,event)'  >

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

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