如何删除焦点输入的默认值
我有一个输入框,分配有默认值文本。当用户聚焦于字段时如何删除此文本::
CoDE
<input type="text" name="kp1_description" value="Enter Keypress Description">
I have an input box that has default value text assigned to it. How can I remove this text when the user focuses on the field::
CoDE
<input type="text" name="kp1_description" value="Enter Keypress Description">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
应该可以做到这一点。
更新了,忘记焦点没有焦点事件的第二个参数,因为没有,它必须与模糊链接:
http://jsfiddle.net/hDCsZ/
您还应该考虑为此创建自己的函数,例如:
然后像这样使用
That should do it.
Updated, Forgot that focus does not have a 2nd parameter for the focus-out event because there is none, it has to be chained with blur:
http://jsfiddle.net/hDCsZ/
you should also think about creating your own function for this such as:
Then use like so
不要这样做。使用 jQuery 水印脚本:
http://code.google.com/p/jquery-watermark/
$("input[name='kp1_description']").watermark("输入按键描述")
;如果您手动执行此操作,则必须考虑很多事情。例如,当文本框失去焦点时会发生什么?如果没有价值,您需要读取帮助文本。如果有,您会希望尊重这些更改。
让其他人做繁重的工作更容易:)
Don't do it this way. Use a jQuery watermark script:
http://code.google.com/p/jquery-watermark/
$("input[name='kp1_description']").watermark("Enter Keypress Description")
;There are a lot of things you have to account for if you do it manually. For instance, what happens when the text box loses focus? If there's no value, you'd want to readd your helper text. If there is, you'd want to honor those changes.
Just easier to let other people do the heavy lifting :)
使用 HTML5,您无需 Javascript 即可完成此操作:只需使用
placeholder
而不是值。
我认为这是一个很好的补充,但当然您需要首先检查浏览器兼容性。
With HTML5 you could do it without Javascript: just use
placeholder
instead ofvalue
.I think it's a nice addition, but of course you need to check the browser compatibility first.
我认为这会有所帮助
i think this will help
var defaultForInput = "abc";
提交表单时,检查是否输入( id="myTextInput") 值是“空字符串”,如果是这样,请将其替换为 (defaultForInput)。
var defaultForInput = "abc";
<input id="myTextInput" type="text" placeholder=defaultForInput />
When submitting your form, check if the input(id="myTextInput") value is the 'empty-string', if so substitute it with (defaultForInput).
超级骗子短版
The Super Duper Short Version
HTML:
Javascript 代码:
就这些,希望对您有所帮助。
HTML:
Javascript code:
That's all, I hope it'll help.
普通 JavaScript:
这会将值保留在占位符中,而不是完全删除它。如果您不喜欢这样,请删除占位符行。
Plain JavaScript:
This keeps the value in the placeholder instead of removing it completely. If you do not like this, remove the placeholder line.