简体   繁体   English

使用Enter键和搜索按钮JavaScript进行搜索

[英]Search using enter key and search button JavaScript

I'm using the following code and it works fine by clicking on the search button. 我正在使用以下代码,单击搜索按钮即可正常运行。 But how can I also search using the enter key? 但是,如何也可以使用Enter键进行搜索?

Code: 码:

 <script> function mySearch() { var text = document.getElementById('strSearchText').value; var url = "privatelink=" + text; window.open(url,'_blank'); } </script> 
 <style> button:hover {color:red;} .rounded-corners { -moz-border-radius: 20px; -webkit-border-radius: 20px; -khtml-border-radius: 20px; border-radius: 20px; white-space:normal; font-weight:bold; width: 50%; } .smallbutton { -moz-border-radius: 20px; -webkit-border-radius: 20px; -khtml-border-radius: 20px; border-radius: 20px; white-space:normal; font-weight:bold; width: 20%; } </style> 
 <br> <input type="text" size="25" tabindex="1" value="" id="strSearchText">&nbsp; <button class="smallbutton" onclick="mySearch();"><span style="DISPLAY: inline-block">Search</span></button> 

Stick it in a form, and it does that by default 粘贴成表格,默认情况下会

<form id="myform">
    <input type="text" size="25" tabindex="1" value="" id="strSearchText">
    <button class="smallbutton">
        <span style="display: inline-block">Search</span>
    </button>
</form>

and then 接着

var form = document.getElementById('myform')

form.addEventListener('submit', function() {
  var text = document.getElementById('strSearchText').value;
  var url  = "privatelink=" + text;

  window.open(url,'_blank');
}, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM