请帮助我,如果用户在文本框中按下 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>
<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";
}
}