开发者

Text auto-complete in text box [image attached inside]

开发者 https://www.devze.com 2023-03-28 06:02 出处:网络
I am designing a website. I am using the following code for a search box: <form method=\"get\" action=\"search.php\">

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?

Text auto-complete in text box [image attached inside]


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

0

精彩评论

暂无评论...
验证码 换一张
取 消