javascript 如果输入字段为空显示 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12317447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If input field is empty show div
提问by Macchiato
I'd like to show the divwith id="showDiv", but only if the inputfield with id="textfield"is empty.
我想显示的DIV与ID =“showDiv”,但只有当输入与现场ID =“文本框”是空的。
<form action="">
<fieldset>
<input type="text" id="textfield" value="">
</fieldset>
</form>
<div id="showDiv" style="width:50px;height:50px;background-color:#6CF;"></div>
Anyone know a Javascript that can do this?
任何人都知道可以做到这一点的Javascript?
回答by kushalbhaktajoshi
回答by Ivan Dyachenko
You can write:
你可以写:
$("#textfield").keyup(function(){
if($(this).val()) {
$("#showDiv").hide();
} else {
$("#showDiv").show();
}
});?
Live example http://jsfiddle.net/Z5sjS/3/
回答by Greg Oks
If (document.getElementById("textfield").value == "")
document.GetElementByZid("showDiv").style.display = "inline"
Else
document.GetElementByZid("showDiv").style.display = "none"
回答by Diomedes
This one works in Chrome and it's pure JavaScript.
这个在 Chrome 中工作,它是纯 JavaScript。
HTML:
HTML:
Enter Text: <input type="text" id="myInput" onkeyup="showColorBox()">
JavaScript:
JavaScript:
<script>function showColorBox() {
var x = document.getElementById("myInput");
var myDiv = document.getElementById("hiddenBox");
if (x.value != "")
{
myDiv.style.display = "block";
} else {
myDiv.style.display = "none";
}
}
</script>
JSFiddle: https://jsfiddle.net/e39vou8s/1/
JSFiddle:https://jsfiddle.net/e39vou8s/1/