How to disable form field using Jquery

Asked

Viewed 3,983 times

5

I have a registration form where the client type the zip code by Request using Ajax to get the address, the neighborhood, the city and the state. But how do I disable these fields after they have been filled with the result of Ajax? I need to disable and enable the fields according to my need and not put in "Read-only".

2 answers

4


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() );

1

You can put in the Success Function of your ajax

$('#idCampo').prop('readonly', true);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.