当在较早的部分中选择特定值时,我想显示输入表单的隐藏部分。
该Javascript为什么起作用
function unhideRecipient() {
var recipDiv = document.getElementById("recipientDetails");
// recipient1 = document.getElementById("recipient1"); // ?????
if (recipient1.value == "0") { // ?????
recipDiv.style.display = "block";
} else {
recipDiv.style.display = "none";
}
}
给出此HTML
<form>
<label>Recipient:</label>
<select name="recip1" id="recipient1" onchange="unhideRecipient()">
<option value="1">Alice Doe</option>
<option value="2">Bobby Poe</option>
<option value="3">Clive Roe</option>
<option value="0">Other ...</option>
</select>
<br />
<div id="recipientDetails">
<label>Name:</label> <input type="text"> <br />
<label>StreetName:</label> <input type="text"> <br />
<label>City:</label> <input type="text"> <br />
</div>
</form>
具有这种风格
label { display: inline-block; width: 6em; text-align: right; }
div#recipientDetails { display: none; }
我曾期望在Javascript中需要注释掉的变量赋值,但似乎仅使用元素id
属性值就好像它是对象名称一样起作用。
我误会了-我在哪里可以读到更多呢?