原型表单字段文本值在焦点上消失

发布于 2024-09-30 17:22:29 字数 541 浏览 2 评论 0原文

我目前正在使用:

<input type="text" value="username" onfocus="this.value=''" name="login" id="login">

在我网站上的登录表单字段中,该字段使用“用户名”一词填充表单,直到用户单击该字段,然后清除该字段。

问题是,如果用户单击该字段,输入一些内容,单击离开,然后单击返回,则会清除他们输入的所有内容!

我已经在我的网站上使用原型,所以我想知道是否有一种方法,使用原型,我可以让第一个“onfocus”清除该字段,但此后再也不会。

这有道理吗?我在谷歌搜索任何资源时遇到困难,有人可以指出我正确的方向吗?

我尝试过:

<input type="text" value="username" onfocus="if (this.value = 'username') this.value = ''" name="login" id="login">

但似乎不起作用

I am currently using:

<input type="text" value="username" onfocus="this.value=''" name="login" id="login">

on my sign in form field on my site, which populates the form with the word "username" until the user clicks on the field, which is then cleared.

The problem is, if the user clicks on the field, types something in, clicks away, and clicks back, it clears whatever they have typed!

I am already using prototype on my site so I was wondering if there was a way, using prototype, I could just have the very first "onfocus" clear the field, but never again after that.

Does this make sense? I am having trouble googling any resources, can someone point me in the right direction?

I tried:

<input type="text" value="username" onfocus="if (this.value = 'username') this.value = ''" name="login" id="login">

and it doesn't seem to be working

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

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

发布评论

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

评论(1

喜爱皱眉﹌ 2024-10-07 17:22:29

仅有条件地清除它:

if (this.value == 'username') this.value = ''

Only conditionally clear it:

if (this.value == 'username') this.value = ''

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