我目前正在嘗試使選擇框僅在用戶從上一個選擇框中選擇特定選項時可見。我一直在嘗試幾個小時,但我似乎無法獲得任何地方。 這裏是我的代碼:如何使選擇框可見,取決於從不同的選擇框中選取的選項
<p>Did you install legacy or X1 cable boxes?</p>
<select id="test4" onchange="myFunction2()">
<option vlaue="" disabled selected>Select your option</option>
<option value="E.A.">Legacy</option>
<option value="E.B.">X1</option>
<option value="both">both</option></option>
</select>
<hr />
<div id = "abc">
<p>How many X1 cable boxes did you install?</p>
<select id="test3">
<option vlaue="" disabled selected>Select your option</option>
<option value="x1">1</option>
<option value="x2">2</option>
<option value="x3">3</option>
</select>
</div>
<script type="text/javascript">
var abcd = document.getElementById("abc");
abcd.style.display = 'none';
var a = document.getElementById("test4").value;
var c = document.getElementById("test3").value;
function myFunction2() {
if (a == "E.B.") {
abcd.style.display = 'block';
}
else {
abcd.style.display = 'none';
}
}
</script>
任何建議,將不勝感激。
編輯: Terrymorse的代碼工作完美,但編輯我的代碼後,我遇到了另一個問題。我可能需要重做我的代碼。
代碼:
<p>Did you install legacy or X1 cable boxes?</p>
<select id="test4" onchange="myFunction2()">
<option vlaue="" disabled selected>Select your option</option>
<option value="legacy">Legacy</option>
<option value="x1">X1</option>
<option value="both">both</option></option>
</select>
<hr />
<div id = "x1Install">
<p>How many X1 cable boxes did you install?</p>
<select id="test3">
<option vlaue="" disabled selected>Select your option</option>
<option value="E.B.x1">1</option>
<option value="E.B.x2">2</option>
<option value="E.B.x3">3</option>
</select>
</div>
<br />
<br />
<div id="legacyInstall">
<p>How many legacy cable boxes did you install?</p>
<select id="test5">
<option vlaue="" disabled selected>Select your option</option>
<option value="E.A.x1">1</option>
<option value="E.A.x2">2</option>
<option value="E.A.x3">3</option>
</select>
</div>
<br />
<br />
<button onclick="myFunction()">Result</button>
<p id="result"></p>
<script type="text/javascript">
var x1Install = document.getElementById("x1Install");
var legacyInstall = document.getElementById("legacyInstall");
x1Install.style.display = 'none';
legacyInstall.style.display = 'none';
var legacyOrX1 = document.getElementById("test4").value;
var x1Box = document.getElementById("test3").value;
var legacyBox = document.getElementById("test5").value;
function myFunction2() {
var legacyOrX1 = document.getElementById("test4").value;
var x1Box = document.getElementById("test3").value;
var legacyBox = document.getElementById("test5").value;
if (legacyOrX1 == "x1") {
x1Install.style.display = 'block';
legacyInstall.style.display='none';
}
else if (legacyOrX1 == "legacy") {
x1Install.style.display = 'none';
legacyInstall.style.display = 'block';
}
else {
x1Install.style.display = 'block';
legacyInstall.style.display = 'block';
}
}
function myFunction() {
var legacyOrX1 = document.getElementById("test4").value;
var x1Box = document.getElementById("test3").value;
var legacyBox = document.getElementById("test5").value;
document.getElementById("result").innerHTML = (x1Box) + " " + (legacyBox);
}
</script>
如果我只選擇X1或唯一遺產,它仍然顯示了我沒有裝框的類型的輸出。我不希望發生這種情況。
'a.value'也工作得很好,沒有必要使用'a.options [a.selectedIndex] .value'。 – terrymorse