<!Doctype html>
<html>
<h1> Java input on Option change </h1>
<body>
<table>
<tr>
<td>option</td>
<td>
<select id="keyselect" onchange="keyPrompt()" >
<option value="0">No</option>
<option value="1">Yes</option>
</select>
</td>
</tr>
</table>
<input name="keys" id="keysneeded" hidden>
<p id="keysneeded"> </p>
</body>
<script>
function keyPrompt()
{
var k = document.getElementByID("keyselect").value;
if (k != 1);{
var keys = prompt("What keys are needed?");
document.getElementByID("keysneeded").innerHTML = keys;
}
}
</script>
</html>
答案 0 :(得分:3)
正如@teemu在评论getElementById
而不是getElementByID
中所说,并为每个控件使用不同的ID。并且在if条件之后删除;
。
function keyPrompt() {
var k = document.getElementById("keyselect").value;
if (k != 1) {
var keys = prompt("What keys are needed?");
document.getElementById("keysneeded").innerText = keys;
document.getElementById("hidInput").value = keys;
}
}
<!Doctype html>
<html>
<h1> Java input on Option change </h1>
<body>
<table>
<tr>
<td>option</td>
<td>
<select id="keyselect" onchange="keyPrompt()">
<option value="0">No</option>
<option value="1">Yes</option>
</select>
</td>
</tr>
</table>
<input name="keys" id="hidInput" hidden>
<p id="keysneeded"></p>
</body>
</html>