所以我正在为历史课做一个谷歌审查的模拟版本。我试图让搜索在按enter时工作,但到目前为止,我得到的只是我输入的第一个字母的搜索。有什么建议吗?
HTML代码:
<form id="searchBox">
<input type="text" id = "search" name="search" size="85" onKeyUp="searchCensor()"/><br />
</form>JavaScript代码:
function searchCensor()
{
var keyTerms = document.getElementById("search").value;
if(keyTerms == "censorship")
window.location = "http://andrewgu12.kodingen.com/history/censor.php";
else if(window.event.keyCode == 13)
window.location = "https://www.google.com/search?q="+keyTerms;
else
window.location = "https://www.google.com/search?q="+keyTerms;
}发布于 2012-04-21 00:26:50
这是你的逻辑:
否则,如果按下的是Enter,则在google上搜索搜索字段的值
否则,在任何其他情况下,在google上搜索搜索字段的值
会发生什么?
所以你最终会在谷歌上搜索字母"e“。
你明白这有什么不对的吗?
也就是说,去掉else,你就会好起来。
发布于 2012-04-21 00:19:13
你需要检查按下的是哪一个键...现在每次按下任何键时,您都在运行您的函数。在您的searchCensor()函数中,您可能希望有一个这样的检查:if (event.keyCode == 13) {
https://stackoverflow.com/questions/10249767
复制相似问题