S
S
Stanislav Konovalov2019-05-29 11:26:17
JavaScript
Stanislav Konovalov, 2019-05-29 11:26:17

Django. More efficient method of hiding fields in a template?

Hello! Now the fields in the search form are hidden with the following condition:

$('#category_id').change(function () {
        var optionSelected = $("option:selected", this);
        var valueSelected = $(this).val();

        if (valueSelected === '1') {
          $('#rooms_id').hide();
          $('#series_id').hide();            
        } else if (valueSelected === '2') {
          $('#rooms_id').hide();
          $('#series_id').hide();
        } else {
          $('#rooms_id').show();
          $('#series_id').show();
        }

Here are the categories, by choosing which unnecessary fields are hidden:
<div class="col-md-3 mb-3">       
                <label class="sr-only">Категории</label>
                <select name="category" class="form-control" id="category_id">
                  <option selected="true" disabled="disabled">Категории</option>
                  {% for category in categories %}
                    <option value="{{ category.pk }}">{{ category.name }}</option>
                  {% endfor %}
                </select>
              </div>

But, it seems to me, this is an incomplete solution to the problem, because categories are added to the template dynamically from the database, and if they are accidentally deleted in the database, and then added in the wrong order, as the selects are now described in the script condition, then nothing will not work. I already had it, I had to add it exactly so that everything worked again, either adjust the condition by changing the selects, or add it to the database in the same half-hour as it was. Who knows a more elegant solution, please tell me.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question