I am designing a website. I am using the following code for a search box:
<form method="get" action="search.php">
<input type="text" id="search" name="search" onKeyUp="showSuggestions();">
<input type="submit" id="sub开发者_如何学JAVAmitsearch" value="Search">
</form>
For the search string entered by the user, I am showing suggestions. My problem is that my web-browser also showing suggestions of previously entered search strings (image below). You can see that Firefox suggestion are in foreground and my search suggestions are in background.
My question: Is there any code which will prevent a text box to remember history? Or anything I can do to fix this?
You can 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;
}
You are telling the browser that you are going to handle the actions for this event.
EDIT: Since this is the event that handles the key press, we need to add that key that is pressed to the input ourselves because we are preventing the default action.
I just saw the source code of a website with similar functionality. It used the following:
<input type="text" id="search" name="search" autocomplete="off">
It worked for me!!!
精彩评论