如果输入值等于特定文本,则显示div [英] if input value equals specific text, display div
本文介绍了如果输入值等于特定文本,则显示div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在缓慢但肯定地学习jQuery,所以请原谅我的无知.我经过大量的搜索,得出了下面的科学怪人示例.
在此示例中,如果用户在 input
中输入"Kasey",我希望div#pete具有 display:block
.
提前谢谢!
<!DOCTYPE html>< html>< head>< script>$(document).ready(function(){var val = $(#fname").length;如果(val =凯西){$(#pete").css("display","block");}</script></head><身体>< p>名称:< input type ="text" id ="fname"></p>< p id ="pete" style ="display:none;"> Pete//p;</body></html>
解决方案
您可以使用 keyup()
处理程序,以监听 keyup
事件.在其中使用 this.value
获取值
$(document).ready(function(){$(#fname").keyup(function(){如果(this.value =="Kasey"){$(#pete").css("display","block");}别的 {$(#pete").css("display","none");}});});
FIDDLE >
更新:
您可以按以下方式简化代码
$(document).ready(function(){$(#fname").keyup(function(){$(#pete").css("display",this.value =="Kasey"?"block":"none");});});
I'm learning jQuery slowly but surely, so please excuse my ignorance. I did a lot of googling that resulted in the Frankenstein example below.
In this example, if the user enters "Kasey" into the input
, I would like div#pete to have display:block
.
Thanks in advance!
<!DOCTYPE html>
<html>
<head>
<script>
$(document).ready(function(){
var val = $("#fname").length;
if (val = Kasey) {
$("#pete").css("display", "block");
}
</script>
</head>
<body>
<p>Name: <input type="text" id="fname"></p>
<p id="pete" style="display:none;" >Pete</p>
</body>
</html>
解决方案
You can use keyup()
handler to listen keyup
event. Inside that use this.value
to get the value
$(document).ready(function() {
$("#fname").keyup(function() {
if (this.value == "Kasey") {
$("#pete").css("display", "block");
}
else {
$("#pete").css("display", "none");
}
});
});
UPADATE :
You can simplify the code as follows
$(document).ready(function () {
$("#fname").keyup(function () {
$("#pete").css("display", this.value == "Kasey" ? "block" : "none");
});
});
这篇关于如果输入值等于特定文本,则显示div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文