你的位置:首页 > Java教程

[Java教程]设置回车的默认按钮detectEnter


场景:

页面有一个搜索文本框和搜索按钮。正常情况下,当我在搜索文本框输入关键字后按回车键就可以触发搜索按钮进行内容搜索,但由于页面上还有其它按钮,而且默认不是搜索按钮,怎样才能实现回车就触发我们的搜索按钮呢?

 

html内容:
 1 <div id="staffSearch"> 2   <div> 3     <asp:TextBox ID="txtStaffName" runat="server" CssClass="txtInput txtStaffName" /> 4     <asp:TextBox ID="txtExt" runat="server" CssClass="txtInput txtExt"  5       onpaste="return false;" onkeyup="this.value=this.value.replace(/\D/g,'')"  6       onafterpaste="this.value=this.value.replace(/\D/g,'')" /> 7     <asp:TextBox ID="txtLocation" runat="server" CssClass="txtInput txtLocation" /> 8     <asp:TextBox ID="txtCompany" runat="server" CssClass="txtInput txtCompany" /> 9     <input type="button" id="btnStaffSearch" class="btnStaffSearch" onclick="staffSearch();" />10   </div>11 </div>


 
脚本内容:
<script type="text/javascript">    //author: Kenmu    //created time: 2014-07-07    //function: 当按回车键时,触发登录按钮事件    //detectEnter begin  function detectEnter(event) {    var e = event || window.event;    var o = e.target || e.srcElement;    var keyCode = e.keyCode || e.which; // 按键的keyCode    if (keyCode == 13) {      e.keyCode = 9;      e.returnValue = false;      $('#btnStaffSearch').click();    }  }  $(function () {    $('#staffSearch').keypress(function (e) { detectEnter(e); });  });  //detectEnter end</script>