简体   繁体   English

从下拉菜单中选择选项的组合以生成结果

[英]Selecting a combination of options from drop-down menus to generate a result

I've managed to generate a piece of text from selecting an option from a drop-down list. 通过从下拉列表中选择一个选项,我设法生成了一段文本。 The Javascript code for this can be found here: 可以在此处找到用于此目的的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
            }
        }

The HTML code for the drop-down list and example content is as follows: 下拉列表和示例内容的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>

My question is whether I can achieve outputting specific pieces of text with combinations of selected options from more than one drop-down list . 我的问题是,是否可以通过从多个下拉列表中选择的选项组合来输出特定文本。 For example, the user could select one option from each of the following drop-down lists, and then click a 'Generate' to generate a workout plan specific to the combination of options that they have chosen: 例如,用户可以从以下每个下拉列表中选择一个选项,然后单击“生成”以生成特定于他们选择的选项组合的锻炼计划:

<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>

I have been trying to extend my existing solution to achieve this, but this has gotten quite messy. 我一直在尝试扩展我现有的解决方案来实现这一目标,但是这变得非常混乱。 Could anyone please advise me on the best way to go about this/or provide some sort of code structure? 有人可以建议我解决此问题的最佳方法/或提供某种代码结构吗? Please note, I am also using phpmyadmin, so I'd welcome any solutions that incorporate this too. 请注意,我也使用phpmyadmin,所以也欢迎任何包含此功能的解决方案。 Please also note that you may have to scroll all the way to the right when reading my code snippets (for some reason it is not formatting/indenting correctly). 另请注意,阅读我的代码段时,您可能必须一直滚动到最右边(由于某种原因,它无法正确格式化/缩进)。

Many thanks for your time. 非常感谢您的宝贵时间。

I would use IDs that correspond with a concatenated selection of the three drop-downs. 我将使用与三个下拉列表的串联选择相对应的ID。

pseudo-code: 伪代码:

targetID = selectionA + selectionB + selectionC

So if you have a selection value of "AAB", you should expose a DIV with an ID of "AAB". 因此,如果选择值为“ AAB”,则应公开ID为“ AAB”的DIV。 Give them all the same class name, hide them by class name, then show the one corresponding with the ID. 给他们全部相同的班级名称,按班级名称隐藏它们,然后显示与ID对应的名称。

<div id="AAB" class="info">...</div>

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

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