按钮自动触发"点击"当按"输入键"

时间:2014-08-14 11:18:48

标签: javascript button click enter onkeyup

在我的网站上,我有一个带按钮的搜索框。在框中搜索时将出现一个自动建议框。我在js上添加了keyup事件,用于上下移动选择。然后输入press,转到js文件进行ajax调用。我也为click事件写过同样的事情。但是,当我使用up or down key选择列表项并按enter key时,它会自动刷新?中的url页面。我正在使用Progressive — Responsive Multipurpose HTML Template。不知道它是否是内置代码。

我的HTML是

<form name="search-form">
        <input class="search-string form-control" type="text" placeholder="Search here" name="" id="navSearchInput" autocomplete="off">
            <button class="search-submit" id="search_submit">
        <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 16 16" enable-background="new 0 0 16 16" xml:space="preserve">
          <path fill="#231F20" d="M12.001,10l-0.5,0.5l-0.79-0.79c0.806-1.021,1.29-2.308,1.29-3.71c0-3.313-2.687-6-6-6C2.687,0,0,2.687,0,6
          s2.687,6,6,6c1.402,0,2.688-0.484,3.71-1.29l0.79,0.79l-0.5,0.5l4,4l2-2L12.001,10z M6,10c-2.206,0-4-1.794-4-4s1.794-4,4-4
          s4,1.794,4,4S8.206,10,6,10z"></path>
        </svg>
      </button>
    </form>

和java脚本代码是

$("#navSearchInput").keyup(TriggerSearch);

function TriggerSearch(e) {
    var search_string = encodeURIComponent($("#navSearchInput").val());
    e = e || window.event;
    var keycode;
    if (window.event) {
        keycode = e.which ? window.event.which : window.event.keyCode;
    }
    var key = e.which;
    switch (key) {
    case 38:
        break;
    case 40:
        break;
    case 13:
        e.preventDefault();
        search_product();
        break;
    default:
        if (search_string != "") {
       //default code
      }

function search_product(e) {
//code goes here
}

它有什么问题?请帮忙......

0 个答案:

没有答案