HTML如何使用javascript清除输入?

时间:2013-06-21 14:29:10

标签: javascript html input erase

我有这个INPUT,每当我们点击它内部时它就会清除。

问题: 我想只清楚value = exemplo@exemplo.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)">

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

8 个答案:

答案 0 :(得分:21)

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

这真的是你想要的吗?

答案 1 :(得分:2)

您可以使用属性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='';}">

答案 2 :(得分: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)" />

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

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

答案 3 :(得分:1)

你不需要为此烦恼。只需写下

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

用占位符替换值

答案 4 :(得分:1)

不是清除名称文字,而是使用占位符属性,这是一种很好的做法

<input type="text" placeholder="name"  name="name">

答案 5 :(得分:0)

试试这个:

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

答案 6 :(得分: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/

答案 7 :(得分:0)

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

<form id="myForm">
  First name: <input type="text" name="fname"><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>