29

我有这个 INPUT,每次我们在里面点击它都会清除。

问题:我只想清除 value = exemplo@exemlo.com

<script type="text/javascript">
    function clearThis(target) {
        target.value= "";
    }
</script>
<input type="text" name="email" value="exemplo@exemplo.com" size="30" onfocus="clearThis(this)">

有人可以帮我做到这一点吗?我不知道如何比较,我已经尝试过但没有成功。

4

8 回答 8

37
<script type="text/javascript">
    function clearThis(target) {
        if (target.value == 'exemplo@exemplo.com') {
            target.value = "";
        }
    }
</script>

这真的是你要找的吗?

于 2013-06-21T14:31:27.817 回答
10

对我来说这是最好的方法:

<form id="myForm">
  First name: <input type="text" name="fname" value="Demo"><br>
  Last name: <input type="text" name="lname"><br><br>
  <input type="button" onclick="myFunction()" value="Reset form">
</form>
     
<script>
function myFunction() {
    document.getElementById("myForm").reset();
}
</script>

于 2020-05-25T02:35:47.750 回答
4

你可以使用属性placeholder

<input type="text" name="email" placeholder="exemplo@exemplo.com" size="30" />

或者在旧浏览器上试试这个

<input type="text" name="email" value="exemplo@exemplo.com" size="30" onblur="if(this.value==''){this.value='exemplo@exemplo.com';}" onfocus="if(this.value=='exemplo@exemplo.com'){this.value='';}">
于 2013-06-21T14:44:22.933 回答
2

您可以使用占位符,因为它会为您完成,但对于不支持占位符的旧浏览器,请尝试以下操作:

<script>
function clearThis(target) {
    if (target.value == "exemplo@exemplo.com") {
        target.value = "";
    }
}
function replace(target) {
    if (target.value == "" || target.value == null) {
        target.value == "exemplo@exemplo.com";
    }
}
</script>
<input type="text" name="email" value="exemplo@exemplo.com" size="x" onfocus="clearThis(this)" onblur="replace(this)" />

代码解释:当文本框有焦点时,清除该值。当文本框未聚焦且该框为空白时,替换该值。

我希望这有效,我一直有同样的问题,但后来我尝试了这个,它对我有用。

于 2016-03-10T18:25:06.067 回答
1

你不需要为此烦恼。写吧

<input type="text" name="email" placeholder="exemplo@exemplo.com" size="30">

用占位符替换值

于 2014-03-14T23:51:18.303 回答
1

而不是清除名称文本使用占位符属性,这是一个好习惯

<input type="text" placeholder="name"  name="name">
于 2017-11-09T05:23:06.480 回答
0

尝试这个 :

<script type="text/javascript">
function clearThis(target){
    if(target.value == "exemplo@exemplo.com")
    {
        target.value= "";
    }
}
</script>

于 2013-06-21T14:31:39.747 回答
0
<script type="text/javascript">
    function clearThis(target){
        if (target.value === "exemplo@exemplo.com") {
            target.value= "";
        }
    }
    </script>
<input type="text" name="email" value="exemplo@exemplo.com" size="30" onfocus="clearThis(this)">

在这里试试:http: //jsfiddle.net/2K3Vp/

于 2013-06-21T14:33:46.743 回答