codingjoe / django-select2

This is a Django integration for Select2
https://django-select2.rtfd.io
MIT License
172 stars 53 forks source link

Use dependent_fields with checkbox #289

Open codingjoe opened 5 months ago

codingjoe commented 5 months ago

Discussed in https://github.com/codingjoe/django-select2/discussions/282

Originally posted by **CleitonDeLima** May 29, 2024 If include a field checkbox in `dependent_fields`, the `'on'` value is sent to the view regardless of whether the field is checked or not. This behavior can be changed [here](https://github.com/codingjoe/django-select2/blob/main/django_select2/static/django_select2/django_select2.js#L30) ```js $.each(dependentFields, function (i, dependentField) { var field = $("[name=" + dependentField + "]", $element.closest("form")); if (field.is(":checkbox")) { result[dependentField] = field.prop("checked"); } else { result[dependentField] = field.val(); } }); ```