我有一个带有四个下拉菜单的屏幕。每个下拉列表都有选项。根据选择,我希望文本“说明”显示在屏幕上。
下拉/选项:
当前,我所有的选项都有一个来自表的“代码”,该表可以正确显示,但描述性不强。例如,如果我选择大米,鸡肉,无和苹果,则屏幕显示:
我要根据所选选项显示说明:
我正在努力显示所选选项(不基于所选选项的描述)的相关代码是:
<script>
function initializeForm() {}
</script>
<body class="Outside" style="background-color:black;" onload="initializeForm();" onkeyup="forminput(event);">
<center>
<table class="Inside" style="height:435px; width:535px;">
<tr class="mainTop">
<th style="height:22px;">Food Menu Options</th>
</tr>
<tr>
<td align="center" valign="middle">
<span style="clear:both;color:darkred; font-weight:bold;">Please Confirm Your Selected Options:<br /> <br /></span>
<div style="float:left; width:28%; text-align:right;">Carbs: </div>
<div style="float:left; width:24%; text-align:left; border-bottom:1px solid black;"> (option1)</div>
<div style="float:left; width:3%;"> </div>
<div style="float:left; width:13%; text-align:right;">Protein: </div>
<div style="float:left; width:24%; text-align:left; border-bottom:1px solid black;"> (option2)</div>
<div style="float:left; width:3%;"> </div>
<div style="float:left; width:28%; text-align:right;">Fat: </div>
<div style="float:left; width:24%; text-align:left; border-bottom:1px solid black;"> (option3)</div>
<div style="float:left; width:3%;"> </div>
<div style="float:left; width:13%; text-align:right;">Fruit: </div>
<div style="float:left; width:24%; text-align:left; border-bottom:1px solid black;"> (option4)</div>
<div style="float:left; width:3%;"> </div>
<div style="clear:both;"> </div>
</td>
</tr>
<tr class="mainBottom">
<th style="height:20px;"> </th>
</tr>
</table>
</center>
</body>
我想学习如何使用函数中的switch语句(在initializeForm函数中?)以及各个下拉列表的div ID / getelementbyID来做到这一点。
我是JS / HTML的初学者,因此给您带来的困惑我深表歉意。只是想弄清楚-谢谢!
答案 0 :(得分:0)
如果您确实想使用<div>
进行操作,则可以执行以下操作。但是,下面的代码仅使用大量代码实现了<select>
标签的一小部分功能。同样,使用<div>
来实现像your这样的表单也不是一个好习惯,因为它会妨碍可访问性。您可以使用简单的<select>
var inputDataFromUser = {
protein: '',
};
function toggleDropdown() {
var optionsDiv = this.querySelector('.option-mock');
optionsDiv.classList.contains('hidden')
? optionsDiv.classList.remove('hidden')
: optionsDiv.classList.add('hidden');
}
function closeDropdown() {
var optionsDiv = this.querySelector('.option-mock');
optionsDiv.classList.add('hidden');
}
function selectValue(name) {
var value = this.textContent;
var displayValueContainer = this.parentNode.previousElementSibling.children[0];
displayValueContainer.textContent = value;
inputDataFromUser[name] = value;
}
function logValueFromSelect() {
var select = document.querySelector('select');
console.log(select.value);
}
.select-mock {
position: relative;
}
.select-mock > div:nth-of-type(2) {
position: absolute;
border: 1px solid cornflowerblue;
width: 100%;
}
.select-triangle {
position: absolute;
right: 0;
}
.option-mock > div {
padding: 0 10px;
background: white;
}
.option-mock > div:hover {
color: white;
background: cornflowerblue;
}
.no-user-select {
user-select: none;
}
.hidden {
display: none;
}
<center>
<table class="Inside" style="height:100px; width:535px;">
<tr>
<td align="center" valign="middle">
<div style="float:left; width:13%; text-align:right;">Protein: </div>
<div
style="float:left; width:24%; text-align:left; border-bottom:1px solid black;"
class="select-mock no-user-select"
tabindex="0"
onclick="toggleDropdown.call(this)"
onblur="closeDropdown.call(this)"
>
<div>
<span> (option2)</span>
<span class="select-triangle">▾</span>
</div>
<div class="option-mock hidden">
<div onclick="selectValue.call(this, 'protein')">None</div>
<div onclick="selectValue.call(this, 'protein')">BT - Butter</div>
<div onclick="selectValue.call(this, 'protein')">OI - Olive Oil</div>
</div>
</div>
<!-- Or you could just do it like this, much simpler -->
<select>
<option value="">None</option>
<option value="BT - Butter">BT - Butter</option>
<option value="OI - Olive Oil">OI - Olive Oil</option>
</select>
<!-- Or you could just do it like this, much simpler -->
</td>
</tr>
</table>
<button onclick="console.log(inputDataFromUser)">Click (value from div)</button>
<button onclick="logValueFromSelect()">Click (value from select)</button>
</center>