Django, отображать форму на основе значения другой формы - PullRequest
0 голосов
/ 05 мая 2020

Я пытаюсь показать или скрыть хрустящую форму, основываясь на значении другой формы.

У меня есть следующая форма:

<div class="form-group col-2 0 mb-0" id="field_three">
{{form.numero_mensilità|as_crispy_field}}
</div>

<div class="form-group col-2 0 mb-0 d-none"  id="field_four">
{{form.mese_pagamento_13|as_crispy_field}}
</div>

Я хочу, чтобы, когда клиент di git в первой форме (id=field_three), автоматически django показывал мне вторая форма (id="field_four"). Это произойдет во время заполнения формы, поэтому, если клиент изменит значение с 13 на 12, например, поле исчезнет.

Я пытался выполнить этот скрипт, но не работает:

// this is executed once when the page loads
    $(document).ready(function() {

        // set things up so my function will be called when field_three changes
        $('#field_three').change( function() {
            check_field_value(this.value);
        });


    // function that hides/shows field_four based upon field_three value
    function check_field_value(new_val) {
        if(new_val != '13') {
            // #id_field_four should be actually the id of the HTML element
            // that surrounds everything you want to hide.  Since you did
            // not post your HTML I can't finish this part for you.
            $('#field_four').removeClass('d-none');
        } else {
            $('#field_four').addClass('d-none');
        }
    }
...