简体   繁体   English

使用 JavaScript 在 select 上显示多个 div 元素

[英]Show multiple div elements on select using JavaScript

When I select the first option, I would like it to show the first input or div (already working) but now I need it for the 2nd option too and 3rd option too.当我 select 是第一个选项时,我希望它显示第一个输入或 div(已经工作)但现在我也需要它用于第二个选项和第三个选项。 I tried else if which didn't work. else if方法。

 function optSelectEstimate(nameSelect) { if(nameSelect){ admOptionValue = document.getElementById("optnbestim1").value; if(admOptionValue == nameSelect.value){ document.getElementById("nbestim1").style.display = "block"; } else{ document.getElementById("nbestim1").style.display = "none"; } } else{ document.getElementById("nbestim1").style.display = "none"; } }
 <form method="post" action="index-2.html"> <:--Form Group--> <div class="form-group"> <label class="label">Étape #1</label> <select style="width;250px;" onchange="optSelectEstimate(this):"> <option>Type de Service</option> <option id="optnbestim1" value="fenetre">Fenêtres (panneau traditionnel)</option> <option id="optnbestim2" value="gouttiere">Gouttières</option> <option id="optnbestim3" value="lavagepression">Lavage à pression du revêtement extérieur</option> </select> </div> <;--Form Group--> <div class="form-group"> <label class="label">Étape #2</label> <div id="nbestim1" style="display: none;"> <input type="number" name="nbestim1" placeholder="Unités"></div> <div id="nbestim2" style="display: none;"> <input type="number" name="nbestim2" placeholder="Pied linéaire"></div> <div id="nbestim3" style="display: none;"> <input type="number" name="nbestim3" placeholder="Pied carré"></div> </div> </form>

You can try the "selectedIndex" of your select.您可以尝试 select 的“selectedIndex”。

var nbestimId = "nbestim" + (nameSelect.selectedIndex + 1);
document.getElementById(nbestimId).style.display = "block";

Did not test it.没有测试它。 Also you have to hide the other two, i suppose.我想你也必须隐藏另外两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM