当有人使用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 :(得分:0)
我在这段代码中发现了两个问题。
首先,事件keydown
被错误拼写为$("#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/
答案 1 :(得分:0)
修复以下内容,它会起作用。
QuickAction
$("#https_urllink").on('keydown', function(){}
事件处理程序中$("#https_urllink").val();
的值,因为该值可能已更改。在keydown
中调用它不会反映更改。$(document).ready()
代替$("#httpstext").html();
。 div没有价值,因此调用$("#httpstext").val();
将无效。请看下面的代码:
.val()
答案 2 :(得分:0)
在您的代码中,urltext始终是&#39;现在删除&#39;。因为你只是在准备好文件时才取价值。你需要获得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**