如何使用javascript清除文本框 [英] How to clear a textbox using javascript

查看:151
本文介绍了如何使用javascript清除文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

<input type="text" value="A new value">

当焦点在$ b $上时,我需要一个javascript方法来清除文本框的值b textbox。

I need a javascript method to clear the value of the textbox when the focus is on the textbox.

如何实现?

推荐答案

它听起来你正试图使用​​水印(当用户专注于盒子时,默认值会自动清除)。确保在清除之前检查该值,否则您可能会删除他们输入的内容!试试这个:

It sounds like you're trying to use a "watermark" (a default value that clears itself when the user focuses on the box). Make sure to check the value before clearing it, otherwise you might remove something they have typed in! Try this:

<input type="text" value="A new value" onfocus="if(this.value=='A new value') this.value='';">

这将确保只有当值为新值时才会清除。

That will ensure it only clears when the value is "A new value".

这篇关于如何使用javascript清除文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆