HTML如何使用javascript清除输入?

Luc*_*raz 9 html javascript 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)">
Run Code Online (Sandbox Code Playgroud)

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

小智 23

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

这真的是你想要的吗?


小智 6

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

<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>
Run Code Online (Sandbox Code Playgroud)

  • 欢迎来到SO!请考虑添加解释,说明为什么您认为这将是OP问题的解决方案,因为它涉及在特定条件下重置输入值。这一点尤其重要,因为其他答案已经涵盖了这个问题。请查看回答[指南](https://stackoverflow.com/help/how-to-answer) (2认同)