[英]How to prevent the trigger of another button
我的ASP.net頁中包含以下內容:
<input type="text" placeholder="Search..." size="30" id="pageLink" />
<br />
<span id="imgGo" class="imgGo floatRight"></span>
JQuery的:
$('.imgGo').click(function () {
var strString = $("#pageLink").val()
var strSearch = "http://www.google.com/search=";
var url = strSearch + strString;
window.open(url, '_blank');
return false;
});
$("#pageLink").keyup(function (event) {
if (event.keyCode == 13 || event.which == 13) {
$(".imgGo").click();
}
});
當我在文本框中輸入內容並單擊imgGo
span時,它可以正常工作,但是如果我按下Enter鍵,則會觸發另一個面板中的搜索按鈕。
我如何可以修改我的jQuery代碼,以確保不發生或添加代碼隱藏,以確保imgGo
觸發只有當輸入按鈕時按下pageLink
文本框具有焦點?
我嘗試了以下操作,但由於imgGo
不是按鈕而無法使用:
<asp:Panel ID="pnlMedSearch" runat="server" DefaultButton="GoImg">
<!--<input type="text" placeholder="Search..." size="30" id="p2" />-->
<asp:TextBox Width="30" ID="pageLink" runat="server" />
<br />
<!--<span id="imgGo" class="imgGo floatRight"></span>-->
<asp:Label ID="imgGo" CssClass="imgGo floatRight" runat="server" />
</asp:Panel>
我收到以下錯誤:
The DefaultButton of 'pnlMedSearch' must be the ID of a control of type IButtonControl.
我想使用不是ASP.net控件的文本框,因為在我的情況下, placeholder
屬性很重要。
我保留了相同的HTML內容,並將Jquery修改為以下內容:
$("#medlineSearch").keypress(function (event) {
if (event.which == 13) {
$("#imgGo").click();
return false;
}
});
添加return false;
確保它沒有調用頁面上的其他任何內容。
替代版本相同。
在任何標簽上注冊為事件:
onkeydown="pressButtonOnEnter('IdOfMyButton', event);"
進入您的.js文件:
function pressButtonOnEnter(buttonId, event) {
var key = (evt.which) ? evt.which : evt.keyCode;
if (key == 13) {
$("#" + buttonId).click();
}
}
它也可以在asp.net中運行。您可以簡單地將其添加到例如asp:textbox中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.