N
N
nik90192020-08-19 10:02:05
JavaScript
nik9019, 2020-08-19 10:02:05

How to hide form field when radio buttons selection changes (in django)?

How can I make the comment field (label + field) only appear when CHOICE1 is selected? How to catch it from javascipt?

There is a form:

class CreateForm(forms.Form):
    ORDER_CHOICES =(
    ("CHOICE1", "CHOICE1"),
    ("CHOICE2", "CHOICE2")
    )
    data = forms.DateField(label="Дата оказания услуги:", widget=forms.DateInput(attrs={'type': 'date'}), required=True)
    time = forms.TimeField(label="Время оказания услуги:", widget=forms.TimeInput(attrs={'type': 'time'}), required=True)
    status = forms.BooleanField(label="Заказ выполнен",initial=False, required=False)
    order_type = forms.ChoiceField(label="Тип услуги:", choices = ORDER_CHOICES, widget=forms.RadioSelect(attrs={'onchange': 'showOrHide("id_order_type_0", "?what?")'}), required=True)
    name = forms.CharField(label="ФИО заказчика:", max_length=60, required=False)
    comment = forms.CharField(label="Комментарий:", widget=forms.Textarea, required=False)


Form and javascript in template:
<form method="POST" action="create/">
        {% csrf_token %}

        <table>
   {{create_order.as_table}}
    </table>

        <input type="submit" value="Сохранить" >
        <input type="submit" value="Отмена" name="cancel">
    </form>

<script type="text/javascript">
  function showOrHide(choice, disp) {
    choice= document.getElementById(choice);
    disp= document.getElementByWhat?(disp);
    if (choice.checked) disp.style.display = "block";
    else disp.style.display = "none";
  }
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-08-19
@bacon

Open the generated html and see if the input has an id, the label has a for, so look for them, or find the parent tr for one of these elements, since there is an as_table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question