gpt4 book ai didi

javascript - 如何设置默认按钮 Html javascript

转载 作者:行者123 更新时间:2023-11-30 00:30:26 25 4
gpt4 key购买 nike

我在一种形式中有 2 个按钮:

<input type="text" id="txtSearch" maxlength="250" placeholder="Enter Search" style="height: 42px; padding-right: 36px;" runat="server" />
<button type="submit" onserverclick="btnSearch_Click" id="btnSearch" runat="server"></button>

<input type="text" id="_searchKeyword" maxlength="250" placeholder="Search Term" style="height: 42px; padding-right: 36px;" runat="server" />
<button type="submit" onserverclick="SearchButton" id="btnSearchNoResult" runat="server">
</button>

我的 JavaScript:

$("#txtSearch").keyup(function (event) {
if (event.keyCode == 13) {
$("#btnSearch").click();
}
});
$("#_searchKeyword").keyup(function (event) {
if (event.keyCode == 13) {
$("#btnSearchNoResult").click();
}
});

如果我在 _searchKeyword 输入文本上键入,然后按 Enter 按钮,我将获得 txtSearch 输入文本的值。如何通过按回车键而不是 txtSearch 的值来获取 _searchKeyword 输入文本的值?

最佳答案

您需要在每个输入中添加类似下面代码的内容..

对于 txtSearch...

onkeydown="if(event.keyCode == 13) doTxtSearch()"

对于搜索关键字...

onkeydown="if(event.keyCode == 13) doSearchKeyword()"

完整代码更改如下....

    <input type="text" id="txtSearch" maxlength="250" placeholder="Enter Search" style="height: 42px; padding-right: 36px;" runat="server" onkeydown="if(event.keyCode == 13) doTxtSearch()"/>
<button type="submit" onserverclick="btnSearch_Click" id="btnSearch" runat="server"></button>

<input type="text" id="_searchKeyword" maxlength="250" placeholder="Search Term" style="height: 42px; padding-right: 36px;" runat="server" onkeydown="if(event.keyCode == 13) doKeywordSearch()"/>
<button type="submit" onserverclick="SearchButton" id="btnSearchNoResult" runat="server">
</button>


function doKeywordSearch()
{
//do Keyword Search stuff here
}

function doTxtSearch()
{
//do Text Search stuff here
}

关于javascript - 如何设置默认按钮 Html javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com