1

请帮助我,如果用户在文本框中按下 ENTER 键,我正在尝试单击隐藏按钮。它适用于一个文本框,但如果我添加另一个,java 脚本不起作用。

文本框。-------------------------------------------------- --------------------------------------

  <pre> 
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>

        <form id="form1" runat="server">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" Text="Button" 
                onclick="Button1_Click1" style="visibility: hidden; display: none;" />

    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <script type="text/javascript">

        var myInput = document.getElementById("TextBox1");
        if (myInput.addEventListener) {
            myInput.addEventListener('keydown', this.keyHandler, false);
        } else if (myInput.attachEvent) {
            myInput.attachEvent('onkeydown', this.keyHandler); /* damn IE hack */
        }

        function keyHandler(e) {
            var EnterKEY = 13;
            if (e.keyCode == EnterKEY) {

                if (e.preventDefault) {

                    document.getElementByID("Button1").click();
                    e.preventDefault();
                }
                return false;
            }
        }
    </script>

        &nbsp;
            <br />
            <asp:Label ID="Label1" runat="server" Visible="False"></asp:Label>
        </form>
    </body>
</pre>
    ------------------------------------------------------------------------------

    CODE BEHIND
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void Button1_Click1(object sender, EventArgs e)
            {
                    TextBox2.Text = "It Works";

            }
        }
4

2 回答 2

2

尝试将您的 javascript 代码包装在 document.onload 函数中

window.document.onload = function(e){
var myInput = document.getElementById("TextBox1");
        if (myInput.addEventListener) {
            myInput.addEventListener('keydown', this.keyHandler, false);
        } else if (myInput.attachEvent) {
            myInput.attachEvent('onkeydown', this.keyHandler); /* damn IE hack */
        }

//adding the second textbox
myInput = document.getElementById("TextBox2");
            if (myInput.addEventListener) {
                myInput.addEventListener('keydown', this.keyHandler, false);
            } else if (myInput.attachEvent) {
                myInput.attachEvent('onkeydown', this.keyHandler); /* damn IE hack */
            }

        function keyHandler(e) {
            var EnterKEY = 13;
            if (e.keyCode == EnterKEY) {

                if (e.preventDefault) {

                    document.getElementByID("Button1").click();
                    e.preventDefault();
                }
                return false;
            }
        }
}

因此,一旦 DOM 树完全加载,您的脚本就会执行

于 2013-04-10T22:10:43.643 回答
0

查看您的代码,您仅将您的keyhandler(e)函数绑定到元素TextBox1,但没有绑定到TextBox2. 尝试为您的TextBox2.

var myInput2 = document.getElementById("TextBox2");
if (myInput2.addEventListener) {
    myInput2.addEventListener('keydown', this.keyHandler, false);
} else if (myInput.attachEvent) {
    myInput2.attachEvent('onkeydown', this.keyHandler); /* IE hack */
}
于 2013-04-16T15:25:00.470 回答