1

我在我的 WordPress 博客中有一个自定义搜索表单,我在文本小部件的侧边栏中有这个搜索代码。

        <div class="search-form">
          <input type="text" value="" class="input-txt" id="search-txt"/>
          <input type="submit" value="Search" class="input-btn" id="idsubmit" onClick="search_redirect()"/>
          <script type="text/javascript">
              function search_redirect(){
                 text_val = document.getElementById('search-txt').value;
                 if(text_val!=''){
                   url_re = 'http://bizisearch.com/search?page=1&limit=20&engine=spider&type=web&query='+'"'+text_val+'"';
                     window.location = url_re;
                   } else {
                     alert('search input is required');
                   }
                   }    

   window.onload = function(){
   document.getElementById('testfield').onkeypress = function(e){
   if (!e) e = window.event;
   var keyCode = e.keyCode || e.which;
   if (keyCode == '13'){
     search_redirect();
     return false;
     }
    }
   }                            
            </script> 
        </div>

我希望搜索结果“在新选项卡中打开”,我可以按“ENTER”键进行搜索....

有没有教程或者你能指导我如何做到这一点???

谢谢

4

1 回答 1

1

You can't control how to open in a new tab. The enter can be accomplished by capturing a click event on your enter key and call your function in that event.

Enter event in JQuery:

$( document ).ready(function() {
    $('.input-btn').keydown(function (e){
        if(e.keyCode == 13){
             search_redirect();
        }
    })
});

Make sure you include JQuery if it is not included: or follow this guide to include it a better way.

In pure Javascript: (you should give your button an id)

window.onload = function(){
   document.getElementById('testfield').onkeypress = function(e){
       if (!e) e = window.event;
       var keyCode = e.keyCode || e.which;
       if (keyCode == '13'){
         search_redirect();
         return false;
       }
   }
}
于 2013-07-09T06:24:21.253 回答