Js控製回車鍵
在ASP.NET網頁中,使用服務器button按鈕,默認第一個button響應回車鍵的點擊事件。
比如
<body>
<form runat="server">
<div>
姓名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
密碼:<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox><br />
<asp:Button ID="btnSubmit" runat="server" Text="登陸"
/>
<input type="reset" value="重置" />
</div>
</form>
</body>
這個表單中就按下回車就默認觸發了btnSubmit_Click事件。
很多時候,第一個按鈕這不是用戶在點擊回車後想做事情。一般來說,回車鍵是完成輸入工作後提交數據如、關閉當前彈出窗口、輸入關鍵字後回車進行搜索。
在ASP.NET網頁中設置回車鍵的解決方法是使用javascript的onkeydown方法捕捉鍵盤點擊事件,使用event.keyCode來獲取用戶點擊的鍵位。
<head>
<title></title>
<link href="css/admin.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript">
function KeyDown() {
var btnsearch = document.getElementByIdx_x_x('<%=btnSearch.ClientID %>');
// 如果按下回車鍵
if (window.event.keyCode == 13) {
btnsearch.click();
//取消回車鍵的默認操作
window.event.returnValue = false
}
}
</script>
</head>
<body onkeydown="KeyDown()">
最後更新:2017-04-02 22:16:08