How to get value from disable dropdown in yii?

486 Views Asked by At

I am using active form in my view like

<?= $form->field($model, 'ishead')
         ->dropDownList(
                array(
                  "1" => "I'm a Head",
                  "0" => "I'm not a head"
                 ), 
                ['prompt'=>'-Select a head type-']
           )
?>

By js, I have set the value and disable that dropdown

$("#register-ishead").val('1');
$("#register-ishead").prop("disabled", true);

When i submit the form, im not able to get that value of disable drop down value.

Is this procedure right? or How to do that

2

There are 2 best solutions below

0
On

First create a hidden field in the form.

After the below code just assign the value to hidden field. then only it will go after submit.

$("#register-ishead").val('1');
$("#register-ishead").prop("disabled", true);

Readonly does not work in dropdown. You have to use disabled and keep the data in hidden field.

0
On

One tricky way is to enable the dropdown just after submitting the form(before sending data to server). Something like this:

$(document).ready(function(){
    $("#YourFormID").submit(function(){
         $("#register-ishead").removeAttr("disabled");
         return true;
    });
});