How to disable field in form using JQuery

5

I have a registration form where the customer types 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 populated with the Ajax result? I need to disable and enable the fields according to my need and not put in "read only".

    
asked by anonymous 22.07.2015 / 20:00

2 answers

4

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() );
    
22.07.2015 / 20:04
1

You can put in the success function of your ajax

$('#idCampo').prop('readonly', true);
    
22.07.2015 / 20:06