我设法通过从下拉列表中选择一个选项来生成一段文字。可以在此处找到Javascript代码:
<script type="text/javascript">
function ShowDiv() {
safeToggleFieldDisplay(document.getElementById('one'), 'none');
safeToggleFieldDisplay(document.getElementById('two'), 'none');
safeToggleFieldDisplay(document.getElementById('three'), 'none');
var dropdown = document.getElementById("ContentListBox");
var index = dropdown.selectedIndex;
var selectedDIV = dropdown.options[index].value;
safeToggleFieldDisplay(document.getElementById(selectedDIV), 'flip');
}
function safeToggleFieldDisplay(field, sVisibility) {
try {
if ((field) && (field.style)) {
if (sVisibility == 'flip') {
if (field.style.display == 'none') {
sVisibility = 'block';
}
else {
sVisibility = 'none';
}
}
field.style.display = sVisibility;
}
}
catch (exception) {
//no handling - just preventing page explosions
}
}
下拉列表和示例内容的HTML代码如下:
<select name="ContentListBox" id="ContentListBox" onchange="javascript:ShowDiv();">
<option value="">Select Macronutrient</option>
<option value="one">Protein</option>
<option value="two">Carbohydrates</option>
<option value="three">Fats</option>
</select>
<div id="one" style="display:none;"> <br>
<img src="Protein.jpg" width="120" height="120" style="position: absolute; bottom: 15px; right: 30px;"/>
<b>About</b> <br>
➢ The most common macronutrient associated with fitness in general. <br>
➢ It repairs the body’s cells.<br>
➢ It forms many body structures, including muscles, skin, and hair.<br>
➢ It maintains and replaces tissues in your body.<br>
➢ It manufactures red blood cells to carry oxygen.<br>
➢ It manufactures antibodies to fight diseases<br>
<b>Examples</b><br>
➢ Lean meat and fish<br>
➢ Eggs and dairy<br>
➢ Beans, nuts, and seeds<br>
➢ Whey, soy, and plant protein supplements<br>
</div>
我的问题是,我是否可以使用多个下拉列表中选定选项的组合来输出特定文本。例如,用户可以从以下每个下拉列表中选择一个选项,然后单击“生成”按钮。生成一个特定于他们选择的选项组合的锻炼计划:
<div class="margins1">
<h2>Goal</h2>
<select name = "goalDrop">
<option>Fat Loss</option>
<option>Lean Muscle</option>
<option>Size & Mass</option>
</select>
<h2>Current Level</h2>
<select name = "levelDrop">
<option>Beginner</option>
<option>Intermediate</option>
<option>Advanced</option>
</select>
<h2>Gym Accessibility</h2> <select name = "gymDrop">
<option>Access to Gym</option>
<option>No Access to Gym</option>
</select>
<h2>Days Per Week</h2> <select name = "weekDrop">
<option>3-Day Split</option>
<option>4-Day Split</option>
<option>5-Day Split</option>
</select></br>
</div>
<div id="generateworkoutplan">
<form action=>
<input type="submit" value="Generate" >
</form>
</div>
我一直在尝试扩展我现有的解决方案以实现这一目标,但这已经非常混乱了。任何人都可以告诉我最好的方法来提供这个/或提供某种代码结构?请注意,我也使用phpmyadmin,所以我也欢迎任何包含此问题的解决方案。另请注意,在阅读我的代码片段时,您可能必须向右滚动(由于某种原因,它没有正确格式化/缩进)。
非常感谢你的时间。
答案 0 :(得分:0)
我会使用与三个下拉列表的连续选择相对应的ID。
伪码:
targetID = selectionA + selectionB + selectionC
因此,如果您的选择值为&#34; AAB&#34;,则应展示ID为&#34; AAB&#34;的DIV。给它们所有相同的类名,按类名隐藏它们,然后显示与ID对应的那个。
<div id="AAB" class="info">...</div>