我正在尝试构建一个类似于下面的表单(为了隐私起见,我更改了内容),如果没有单独的下拉框相互抵消,我无法理解如何构建它。
很抱歉,我对JavaScript一无所知。我找到了这段代码,修改了我需要的值,并建立了第二个下拉列表,这就是我了解到“OnChange”似乎会取消当前选择的任何内容的时候。在我将要使用的实际形式中,我将有多达5个不同的下拉选项。
function showPara(val) {
// check if the value is not empty or undefined
if (val !== undefined && val !== "") {
// then selelct all the p element which have a common class and add the coass which hide the element
document.querySelectorAll(".pClass").forEach(function(item) {
item.classList.add("hidePara");
// remove the class hide element where the id of p matches with the selected value
document.getElementById(val).classList.remove('hidePara')
})
}
}
.hidePara {
display: none;
}
.pClass {
color: blue;
}
<form>
Favorite Breakfast
<br>
<select onchange="showPara(this.value)">
<option>Select</option>
<option value='6A'>Pancakes</option>
<option value='6B'>Waffles</option>
</select>
<br>
<br> Favorite Juice
<br>
<select onchange="showPara(this.value)">
<option>Select</option>
<option value='6C'>Orange</option>
<option value='6D'>Grape</option>
</select>
</form>
<div>
Alert - Garmin Terms Violation
<br><br> Dear Breakfast Club Member,
<br><br> We want to take a quick survey for the upcoming breakfast event. Please select an option for both questions.
<br><br> Favorite Breakfast:
<!-- ------------------------------------------------------------------------------------------------- -->
<span id="6A" class="pClass hidePara">Pancakes</span>
<span id="6B" class="pClass hidePara">Waffles</span>
<!-- ------------------------------------------------------------------------------------------------- -->
<br> Favorite Juice:
<!-- ------------------------------------------------------------------------------------------------- -->
<span id="6C" class="pClass hidePara">Orange</span>
<span id="6D" class="pClass hidePara">Grape</span>
<!-- ------------------------------------------------------------------------------------------------- -->
<br><br> Thank you so much for participating.
<br><br> Regards,
<br> Breakfast Club President
</div>
我试图将“OnChange”更改为“OnSelect”,但这并没有解决问题。
我希望能够让某人选择他们最喜欢的早餐下拉框,当他们选择他们最喜爱的果汁时,不清除为最喜爱的早餐选择的第一个值。