HTML:搜索 onblur onfocus defaultvalue 和 onsubmit

发布于 2024-10-07 22:11:06 字数 311 浏览 1 评论 0原文

我正在尝试构建一个默认值“搜索此处”的搜索框,例如,onfocus 它将清除它,而 onblur 如果其为空,它将带回“搜索此处”。

为此,我可以使用:

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

这工作得很好,但是当我点击搜索而不输入文本时,它会搜索“搜索此处”。

在搜索之前删除默认值的最佳方法是什么?

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 removing the default value before searching?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌能量女王 2024-10-14 22:11:06

使用 HTML5 占位符属性。就像这样:

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

Use HTML5 placeholder attribute. Like so:

<input type="text" placeholder="Search here"/>
几味少女 2024-10-14 22:11:06

如果您希望用户能够搜索并清空搜索查询,您只需捕获后端(php/asp/whatever)上的默认搜索字符串并将其替换为空字符串即可。

或者,如果您想阻止搜索,如果默认值位于搜索框中,您可以使用 javascript 来阻止提交。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文