2016-05-15 76 views
0

我需要清除文本和隱藏圖像,當有人刪除使用jquery的輸入文本內的消息這裏是我的代碼,但現在不工作現在有人向我展示樣本片段?jquery輸入自動清除文本

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
    <script> 
    $(document).ready(function(){ 
    var urltext = $("#https_urllink").val(); 
    $("#https_urllink").onkeydow(function(){ 
    if(urltext == ""){ 
    $("#httpstext").text(""); 
    $("#httpsimage").hide(); 
    }else if(urltext != "www.ilik.com"){ 
$("#httpstext").text("hello no"); 
} }); }); 
    </script> 

這裏是我的HTML

<img src="hmm.png" id="httpsimage"/> 
<div id="httpstext">See her</div> 
<input type="text" id="https_urllink" value="delete now"/> 
+0

onkeydow(函數(){??應的onkeydown(函數(){ – Poria

+0

也嘗試檢查空值一樣,如果(urltext.length <1) – Poria

+0

它沒't工作@Poria – Micheal

回答

0

我發現在這兩個代碼的問題。

首先,事件​​拼錯爲$("#https_urllink").onkeydow(function(){,並且還包括on,這不是必需的。

其次,當你使用input type="text"時,jquery會像用戶那樣更新字段。所以,而不是使用$("#httpstext").text("");我們可以使用$("#httpstext").val("");

下面的代碼可能會解決您的問題。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
urltext = $("#https_urllink").val(); 
$("#https_urllink").keydown(function(){ 
if(urltext == ""){ 
$("#httpstext").val(""); 
$("#httpsimage").hide(); 
     } 
    }); 
}); 
</script> 

更新

$(document).ready(function() { 
    $("#https_urllink").keydown(function() { 
    if ($("#https_urllink").val() == "") { 
     $("#httpstext").val(''); 
     $("#httpsimage").hide(); 
     return false; 
    } else if($("#httpstext").val() != "www.ilik.com") { 
     $("#httpstext").val('hello no'); 
     $("#httpsimage").hide(); 
     return true; 
    } 
    }); 
}); 

上面的代碼是現在工作。你可以在這裏檢查小提琴。 https://jsfiddle.net/b0p14sfa/1/

+0

它沒有在這裏工作,我編輯了我關於urltext的問題@Kirs – Micheal

+0

另一個重要的確認是你檢查字段值是否正確?因此,下面的代碼if(urltext ==「」) {'只有當$(「#https_urllink」)的值爲空時纔會執行,這些字段是a已經空了,你正在清空空白字段的值。在這裏進行檢查是沒有意義的。請重新考慮您的要求。 –

+0

它假設是'if(urltext!=「」){'。請試試這個。 –

0

修復以下問題,它將起作用。

  1. 使用$("#https_urllink").on('keydown', function(){}
  2. 檢查的$("#https_urllink").val();的​​事件處理程序裏面的值值可能已經改變。在$(document).ready()中調用它將不會反映這些更改。
  3. 使用$("#httpstext").html();而不是$("#httpstext").val();。 divs沒有價值,所以調用.val()將無法​​正常工作。

看看下面的代碼:

$(document).ready(function(){ 
    $("#https_urllink").on('keydown', function(){ 
     urltext = $("#https_urllink").val(); 
     if(urltext == ""){ 
      $("#httpstext").html(""); 
      $("#httpsimage").hide(); 
     } 
    }); 
}); 
0

在你的代碼urltext總是 '立即刪除'。因爲只有在文檔準備好的情況下才能獲得價值。你需要獲得keydown事件的價值,然後才能正常工作。

另外我建議你使用keyup而不是keydown。

我已經更新了代碼。請有嘗試,希望它會幫助你

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $("#https_urllink").keyup(function(){ 
     var urltext = $("#https_urllink").val(); 
    console.log(urltext);  
    if(urltext == ""){ 
     $("#httpstext").text(""); 
     $("#httpsimage").hide(); 
     return false; 
    } 
    else if(urltext != "www.ilik.com"){ 
     $("#httpstext").text("hello no"); 
     $("#httpsimage").show(); 
     return true; 

    } 
}); }); 
</script> 

<img src="hmm.png" id="httpsimage" height="150" width="100"/> 
<div id="httpstext">See her</div> 
<input type="text" id="https_urllink" value="delete now"/>**strong text**