空格键和enter键的事件处理(页面存在多个按钮,登陆按钮不是页面的第一个按钮)

来源:互联网 发布:s1728gwr-4p 端口镜像 编辑:程序博客网 时间:2024/05/03 20:51

<script type="text/javascript">
    function keydown(e)
    {
        if(e.keyCode===13)
        {
            if(window.event)          //IE浏览器
            {e.returnValue=false;}
            else                      //FireFox浏览器
            {e.preventDefault();}
            document.getElementById('ctl00_ContentPlaceHolder2_submit').click();  //   ctl00_ContentPlaceHolder2_submit为submit登陆按钮的客户端名称      
        }
    }
    </script>
html:
在需要触发事件的控件上,添加onkeydown,例如:
账号:<asp:TextBox ID="username" runat="server"  onkeydown="keydown(event);"></asp:TextBox>
密码:<asp:TextBox ID="pwd" runat="server" TextMode="Password" onkeydown="keydown(event);"></asp:TextBox>
登陆按钮:<asp:ImageButton ID="submit" runat="server" ImageUrl ="xxx.gif" alt="登陆按钮" onkeydown="keydown(event);" />

原创粉丝点击