日期:2014-05-18  浏览次数:20489 次

敲击回车触发指定按钮事件,兼容ie,ff等
我的前台页面里有两个textbox,还有对应的两个button,我想当在不同的textbox输入时触发不同的按钮,请问如何实现,要兼容ie ,ff。我现在的只能兼容ie
万分感谢!

------解决方案--------------------
HTML code
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
  TextBox1.Attributes.Add("onkeydown", "k(event)");
  TextBox2.Attributes.Add("onkeydown", "k(event)");
}

protected void Button1_Click(object sender, EventArgs e)
{
  Response.Write("点击1");
}

protected void Button2_Click(object sender, EventArgs e)
{
  Response.Write("点击2");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title></title>
  <script type="text/javascript">
    function k(e) {
      e = e || window.event;
      evt = window.event ? window.event.srcElement : e.target;
      if (e.keyCode.toString() == "13") {
        if (evt.id == "TextBox1") {
          document.getElementById("Button1").focus();
          document.getElementById("Button1").click();
        }
        else {
          document.getElementById("Button2").focus();
          document.getElementById("Button2").click();
        }
      }
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
  <asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
  </form>
</body>
</html>