Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a drop down and still submit a value?

Tags:

jquery

I have a drop down box that I would like to lock after an item has been selected. Unfortunately using 'disable' stops the field from being submitted at all when the form is submitted. Is there a way around this?

like image 221
Abram Avatar asked Dec 04 '22 16:12

Abram


2 Answers

You can re-enable the dropdown list right before the form is submitted:

$("form").submit(function() {
    $("#yourDropdown").prop("disabled", false);
});
like image 70
Frédéric Hamidi Avatar answered Dec 26 '22 23:12

Frédéric Hamidi


Take a hidden field:

<input id="hiddenSelect" type="hidden" name="same_as_select_box">

Then in select change event set the value to that hidden field like following:

$('select').change(function() {
  $('#hiddenSelect').val(this).val();
  $(this).prop('disabled', true);
});

Now you can submit the form with your select value, without further enabling it at submit.

like image 45
thecodeparadox Avatar answered Dec 27 '22 00:12

thecodeparadox