开发者

Text box value sticks even if i refresh?

开发者 https://www.devze.com 2022-12-17 16:47 出处:网络
i have the following html : <input type=\"text\" id=\"searchbox\" name=\"q\" value=\"Search Pictures...\" onclick=\" if(this.value==\'Search Pictures...\'){this.value=\'\';}\" />

i have the following html :

<input type="text" id="searchbox" name="q" value="Search Pictures..." onclick=" if(this.value=='Search Pictures...'){this.value='';}" />

I开发者_运维技巧t's works when i open the page for the first time, but when i type something and search , then i come back to the page, or refresh, i find the past keyword still sticky instead of "Search Pictures..."

Any available tag to avoid this problem ?

Thanks


If you've set the value on the tag to "Search Pictures..." as you have, then whenever the page loads without having somehow persisted a newer value beforehand, you will get the "Search Pictures..." as the value.

To persist input data, you have several ways available depending on what environment you're developing with.


By the way... don't write:

this.value === 'blah blah, copy of text from value attribute'

Just simply use defaultValue property:

this.value == this.defaultValue

It's much simpler and cleaner.


I guess it's on firefox.

You can add the attribute autocomplete="off"


HTTP is stateless and does not persist form values between requests.

Every single time you load the page the 'hard-coded' VALUE 'Search Pictures...' will replace anything in the input element from the previous request. You need to persist the value by retrieving the posted value from the form collection and writing it back in. How you do this depends on the platform you are working on - presumably you are using some kind of server-side framework or language (e.g. PHP or ASP.NET)?

0

精彩评论

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