我正在设计一个网站。我将以下代码用于搜索框:
<form method="get" action="search.php">
<input type="text" id="search" name="search" onKeyUp="showSuggestions();">
<input type="submit" id="submitsearch" value="Search">
</form>
对于用户输入的搜索字符串,我展示的是建议。我的问题是我的网络浏览器还显示了之前输入的搜索字符串的建议(下图)。您可以看到 Firefox 建议在前台,我的搜索建议在后台。
我的问题:是否有任何代码可以阻止文本框记住历史记录?或者我能做些什么来解决这个问题?
最佳答案
您可以 prevent the default Action :
function onKeyDown(e) {
e.preventDefault();
var code;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
e.target.value += character;
return false;
}
您是在告诉浏览器您将处理此事件的操作。
编辑:由于这是处理按键的事件,我们需要自己将按下的那个键添加到输入中,因为我们要阻止默认操作。
关于php - 文本框中的文本自动完成[内附图片],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7059697/