Alternative to disabling select box/dropdown for Django post

Validating a form and disabling it so users can’t enter values is a common choice. But with Django model forms, if the field is mandatory – the form won’t post. It’s easy to use ‘readonly’ properties for text fields, but it becomes a little complicated for the select boxes. Learned a novel way of handling selected selext boxes is a 2 step process:

1. Set the value of the field you’d like to stick with

$( "#id_of_select_option" ).val("{{l_status_disabled}}");

2. Disable all options in the select/dropdown apart from the one selected

$( "#id_of_select option:not(:selected)" ).prop('disabled',true);

To undo the operation and enable all options in the select/dropdown:

$( "#id_of_select option" ).prop('disabled',false);
Advertisements

One thought on “Alternative to disabling select box/dropdown for Django post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s