[英]Prevent unwanted button click when pressing “enter”
我有兩個文本框,每個文本框旁邊都有一個按鈕,當用戶單擊其中一個文本框內的enter時,應單擊它旁邊的按鈕( onkeydown
javascript代碼)。 問題是,當用戶單擊Enter時,無論哪個文本框具有焦點,都將單擊“ btnSource_Type
”按鈕。
我嘗試從txtSource_Type
文本框中刪除“ onkeydown
”事件,但仍然單擊了BtnSource_Type
。
如何只允許單擊指定的按鈕而不單擊頁面中的第一個按鈕?
<tr>
<td width="20%"> Source Type</td>
<td width="80%">
<asp:TextBox ID="txtSource_Type" runat="Server" Text='<%# Bind("Source_Type_Name") %>' Width="90%" onkeydown="if (event.keyCode == 13) document.getElementById('MainContent_frmArticle_Source_btnSource_TypeGridView').click();" CssClass="textbox" style=" font-family: 'Segoe UI',Verdana,Helvetica,sans-serif !important; font-size: small !important;font-weight: normal !important;" />
<asp:ImageButton ID="btnSource_Type" OnClientClick=" alert('Source Btn Clicked');" OnClick="btnSource_TypeClick" runat="server" ImageUrl="~/Images/find1.png" Width="3%" />
</td>
</tr>
<tr>
<td width="20%"> Author Name</td>
<td width="80%">
<asp:TextBox ID="txtAuthor" runat="Server" Text='<%# Bind("Author_Name") %>' Width="90%" CssClass="textbox" onkeydown="if (event.keyCode == 13) document.getElementById('MainContent_frmArticle_Source_btnAuthor').click();" style=" font-family: 'Segoe UI',Verdana,Helvetica,sans-serif !important; font-size: small !important;font-weight: normal !important;" />
<asp:ImageButton ID="btnAuthor" OnClick="btnAuthor_Click" runat="server" ImageUrl="~/Images/find1.png" Width="3%" />
</td>
</tr>
hope this code helps you:
$('#txtSource_Type').bind("keyup keypress", function(e) {
var code = e.keyCode || e.which;
if (code == 13) {
$('#btnSource_Type').click(function(e) {
perform_task();
}
}
});
同樣
$('#txtAuthor').bind("keyup keypress", function(e) {
var code = e.keyCode || e.which;
if (code == 13) {
$('#btnAuthor').click(function(e) {
perform_task();
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.