【问题标题】:HTML how to clear input using javascript?HTML如何使用javascript清除输入?
【发布时间】:2023-04-09 13:16:01
【问题描述】:

我有这个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)">

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

【问题讨论】:

  • 您正在寻找placeholder 属性。
  • IE8 本身并不支持placeholder 属性

标签: javascript html input erase


【解决方案1】:
<script type="text/javascript">
    function clearThis(target) {
        if (target.value == 'exemplo@exemplo.com') {
            target.value = "";
        }
    }
</script>

这真的是你要找的吗?

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 欢迎来到 SO!请考虑添加解释,说明为什么您认为这将是 OP 问题的解决方案,因为它涉及在特定条件下重置输入值。这一点尤其重要,因为其他答案已经涵盖了这个问题。看看回答guidelines
    【解决方案3】:

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

    【讨论】:

    • 那么 IE 9 和更早版本呢?
    【解决方案4】:

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

    <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)" />
    

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

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

    【讨论】:

      【解决方案5】:

      您无需为此烦恼。随便写

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

      用占位符替换值

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

          【解决方案7】:

          试试这个:

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

          【讨论】:

            【解决方案8】:
            <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/

            【讨论】:

              猜你喜欢
              • 2010-12-14
              • 1970-01-01
              • 1970-01-01
              • 2020-02-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-06-21
              相关资源
              最近更新 更多