我的 onkeypress 事件在我网站的搜索栏上不起作用。这是我的 javascript 和 html 代码:
window.onload = function() {
document.getElementById("SearchText").value = getParameterByName("s");
var a = document.getElementById("SearchLink");
a.onclick = function() {
search();
}
}
function search() {
var searchResultsUrl = "/search/";
document.location.href = searchResultsUrl + "?s=" +
document.getElementById("SearchText").value;
return false;
}
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)')
.exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
这是html代码:
<input type="text" id="SearchText" onkeypress="if(SearchText.keyCode==13)search()" />