Linked Questions

Popular Questions

What script function will trigger a reload of the modelform after a onchange event in a dropdown?

Below, I am using 'product_type' as a drop down choice field. I have given it css_id='selector'

The modelform contains a conditional script based upon this selector (from post Conditional fields in a ModelForm.Meta)

The form changes given the choice and therefore I would need to update/reload to reflect the choice made in the drop-down. Which script function will do the trick in this case?

in my html,

<script>

$("#selector").on("change", function() {..???.});

</script>

Code:

class ProductForm(ModelForm):

    class Meta:
        model = AllProduct
        fields = ('__all__')

    def __init__(self, *args, **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-sm-2'
        self.helper.field_class = 'col-sm-4'
        self.helper.form_id = 'All-product'
        self.helper.form_method = 'post'
        self.helper.form_action = ('submit_form')
        self.helper.add_input(Submit('submit', 'Submit', css_class='btn-success'))

        self.helper.layout = Layout(
            Field('product_type', css_class='form-control', css_id='selector'),
            Field('car_make', css_class='select2'),
            Field('boat_make', css_class='select2'),
            Field('number_wheels', step=1, min=0),
            Field('number_sails', step=1, min=0),
             )


        product = 'product_type'

        if product == 'Car':
            fields_to_delete = ('boat_make', 'number_sails')
        else:
            fields_to_delete = ('car_make', 'number_wheels')
        for field in fields_to_delete:
            del self.fields[field]

Related Questions