When you want to disable the field, simply change disabled
to true
or false
to enable or disable.
$("#campo").prop( "disabled", true );
$("#campo").prop( "disabled", false );
UPDATE: readonly disabled
You can use readonly
or disabled
, both prevent the user from changing the field, but disabled
prevents the value of the field from being used anyway, that is, when the form is sent, the value field will not be sent together. However, some fields such as <SELECT>
, <OPTION>
and <BUTTON>
do not have the readonly
attribute, so using readonly
with these fields will not work.
So you can use readonly
instead of disabled
for fields that are not <SELECT>
, <OPTION>
or <BUTTON>
. If you have any of these in your form, you can use disabled
and have a hidden
field that will contain the value of the disabled form of the form to be sent together with the form. Being .campos
the fields that can be used When you want to "disable" the fields:
$(".campos").prop( "disabled", true );
$(".select").prop( "readonly", true );
$(".escondido").val( $(".select").val() );