如何设置默认按钮Html javascript

时间:2015-04-22 03:14:19

标签: javascript html asp.net

我有一个表格中的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输入文本的值?

1 个答案:

答案 0 :(得分:1)

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

对于txtSearch ...

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

对于searchKeyword ...

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
    }