2

我想用id="showDiv"显示div,但前提是id="textfield"的输入字段为空。

<form action="">
 <fieldset>
  <input type="text" id="textfield" value="">
 </fieldset>
</form>

<div id="showDiv" style="width:50px;height:50px;background-color:#6CF;"></div>

任何人都知道可以做到这一点的Javascript?

4

4 回答 4

4

尝试这个

<script type="text/javascript">
if(document.getElementById("textfield").value == ""){
document.getElementById("showDiv").style.display="block";
}
</script>

检查JSFiddle以获取示例

于 2012-09-07T11:50:35.270 回答
4

你可以写:

$("#textfield").keyup(function(){
    if($(this).val()) {
        $("#showDiv").hide();
    } else {
        $("#showDiv").show();
    }

});​

现场示例http://jsfiddle.net/Z5sjS/3/

于 2012-09-07T11:52:12.373 回答
0
If (document.getElementById("textfield").value == "")
 document.GetElementByZid("showDiv").style.display = "inline"
Else
 document.GetElementByZid("showDiv").style.display = "none"
于 2012-09-07T11:53:34.330 回答
0

这个适用于 Chrome,它是纯 JavaScript。

HTML:

Enter Text: <input type="text" id="myInput" onkeyup="showColorBox()">

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/

于 2018-02-16T15:37:20.543 回答