开发者

HTML: search onblur onfocus defaultvalue and onsubmit

im trying to build a search box with a default value "Search Here" for example, onfocus it will clear it, and onblur if its empty it will bring back "Search Here".

for this i can use:

onfocus="if (this.value==this.defaultValue) this.value = ''";
onblur="if (this.value=='') this.value = this.defaultValue";

this works just fine, but when i hit search without inputing a text, it searchs for "Search Here".

what is the best way of 开发者_Python百科removing the default value before searching?


Use HTML5 placeholder attribute. Like so:

<input type="text" placeholder="Search here"/>


If you want a user to be able to search for and empty search query, you can just catch the default search string on the back-end (php/asp/whatever) and replace it with an empty string.

Or you can use javascript to prevent the submit if the defaultvalue is in the searchbox if you want to prevent the search.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜