Html HTML如何使用javascript清除输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17237772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:20:19  来源:igfitidea点击:

HTML how to clear input using javascript?

javascripthtmlinputerase

提问by Lucas Ferraz

I have this INPUT, it will clear everytime we click inside of it.

我有这个输入,每次我们点击它时它都会清除。

The problem: I want to clear only if value = [email protected]

问题:我只想在 value = [email protected] 时清除

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

Can someone help me to do this? I don't know how to compare, I already tried but no success.

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

回答by David Scott

<script type="text/javascript">
    function clearThis(target){
        if(target.value=='[email protected]'){
        target.value= "";}
    }
    </script>

Is this really what your looking for?

这真的是你要找的吗?

回答by SubLock69

You could use a placeholder because it does it for you, but for old browsers that don't support placeholder, try this:

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

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

CODE EXPLAINED: When the text box has focus, clear the value. When text box is not focused AND when the box is blank, replace the value.

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

I hope that works, I have been having the same issue, but then I tried this and it worked for me.

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

回答by Bugaloo

you can use attribute placeholder

你可以使用属性 placeholder

<input type="text" name="email" placeholder="[email protected]" size="30" />

or try this for older browsers

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

<input type="text" name="email" value="[email protected]" size="30" onblur="if(this.value==''){this.value='[email protected]';}" onfocus="if(this.value=='[email protected]'){this.value='';}">

回答by Duck

You don't need to bother with that. Just write

你不需要为此烦恼。写就好了

<input type="text" name="email" placeholder="[email protected]" size="30">

replace the value with placeholder

用占位符替换值

回答by Sai Gopi N

instead of clearing the name text use placeholderattribute it is good practice

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

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

回答by Juan Prendas

For me this is the best way:

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

<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>

回答by Raitom

Try this :

尝试这个 :

<script type="text/javascript">
function clearThis(target){
    if(target.value == "[email protected]")
    {
        target.value= "";
    }
}
</script>

回答by Adam

<script type="text/javascript">
    function clearThis(target){
        if (target.value === "[email protected]") {
            target.value= "";
        }
    }
    </script>
<input type="text" name="email" value="[email protected]" size="30" onfocus="clearThis(this)">

Try it out here: http://jsfiddle.net/2K3Vp/

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