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