簡體   English   中英

HTML如何使用javascript清除輸入?

[英]HTML how to clear input using javascript?

我有這個輸入,每次我們點擊它時它都會清除。

問題:我只想在 value = exemlo@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)">

有人可以幫我做到這一點嗎? 我不知道如何比較,我已經嘗試過但沒有成功。

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

這真的是你要找的嗎?

對我來說,這是最好的方法:

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

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

您可以使用占位符,因為它適合您,但對於不支持占位符的舊瀏覽器,請嘗試以下操作:

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

代碼解釋:當文本框獲得焦點時,清除該值。 當文本框未聚焦且框為空白時,替換值。

我希望這有效,我一直有同樣的問題,但后來我嘗試了這個,它對我有用。

你不需要為此煩惱。 隨便寫

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

用占位符替換值

而不是清除名稱文本使用占位符屬性,這是一種很好的做法

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

試試這個 :

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM