使用回车键和搜索按钮JavaScript进行搜索

时间:2015-04-11 13:10:32

标签: javascript jquery search

我正在使用以下代码,单击搜索按钮可以正常工作。但是我怎样才能使用回车键进行搜索?

代码:

<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>

1 个答案:

答案 0 :(得分:0)

将其粘贴在表格中,默认情况下会这样做

<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>

然后

var form = document.getElementById('myform')

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

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