简体   繁体   English

根据先前的选择显示/隐藏下拉菜单

[英]Show/Hide dropdown menu based on previous selection

I would like to show an additional dropdown menu based on a user's selection.我想根据用户的选择显示一个额外的下拉菜单。 Using my case, if a user selects installments an additional dropdown menu would appear, it would then be hidden if the user changes its selection.以我为例,如果用户选择分期付款,则会出现一个额外的下拉菜单,如果用户更改其选择,它将被隐藏。

This is what I have tried这是我尝试过的

            <select name='installments' required class="form-control my-2" onclick="choose()">
                <option value="" disabled selected hidden>How would you like to pay?</option>
                {% for value, name in form.fields.installments.choices %}
                    <option value="{{value}}">{{name}}</option>
                {% endfor %}
            </select>
            <!-- if installments -->
            <select name='installnum' class="form-control my-2" style="display:none">
                <option value="" disabled selected hidden>How many months would you like to pay this over?</option>
                {% for value, name in form.fields.installmnum.choices %}
                    <option value="{{value}}">{{name}}</option>
                {% endfor %}
            </select>

This is the javascript I wrote这是我写的 javascript

<script>
    function choose() {
        if (document.getElementById('installments').value == 'installments') {
            document.getElementById('installnum').style.display == 'block';
                } else {
                    document.getElementById('installnum').style.display = 'none';
                }
            }
</script>

The idea is supposed to be if you select installments the dropdown menu installnum would appear这个想法应该是如果你 select 分期付款下拉菜单 installnum 会出现

This is my forms.py with the list of options for each dropdown menu这是我的 forms.py,其中包含每个下拉菜单的选项列表

installment_choices = (
    ('upfront', 'Upfront'),
    ('installments', 'Installments'),
)
installnum_choices = (
    ('2', '2 months'),
    ('3', '3 months'),
    ('4', '4 months'),
    ('5', '5 months'),
    ('6', '6 months'),
    ('7', '7 months'),
    ('8', '8 months'),
    ('9', '9 months'),
    ('10', '10 months'),
)

I'm not entirely sure this answers the question, but it looks like you're trying to access the DOM element and read it's value directly.我不完全确定这回答了这个问题,但看起来您正在尝试访问 DOM 元素并直接读取它的值。 I believe you need to look at the value generated by the event.我相信你需要看看事件产生的价值。

function choose (event)
{
   event.preventDefault();
   if(event.value === "expected") { // ... then modify the css }
}

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

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