1
I am trying to do the following: the user type his zip code, and then it is filled automatically, estado
(select) cidade
(select), rua
(input) and bairro
(input), when the user enters his zip code and for example is a complete zip (City, State, Street and Neighborhood) the fields were disabled for editing using Disable
. When the zip code is only of the City (City and state) the fields Neighborhood and street are enabled to type. For this was using this script:
$("#ZipCode").change(function () {
var zipCodeValue = $(ZipCode).val();
var url = '@Url.Action("ReturnZipCode", "Ajax")';
$.ajax({
type: 'POST',
url: url,
data: { zipCode: zipCodeValue },
dataType: 'json',
success: function (data) {
document.getElementById("City").disabled = true;
document.getElementById("State").disabled = true;
$('#State').val(data.StateID).change();
setTimeout(setCity, 1000);
function setCity() {//insere a cidade após 3 segundos
$('#City').val(data.CityID).change();
$('#CityID').val($('#City').val());
}
if (data.AddressName != "") {
document.getElementById("Address").disabled = true;
$('#Address').val(data.AddressName);
} else {
document.getElementById("Address").disabled = false;
$('#Address').val("");
}
if (data.Neighborhood != "") {
document.getElementById("Neighborhood").disabled = true;
$('#Neighborhood').val(data.Neighborhood);
} else {
document.getElementById("Neighborhood").disabled = false;
$('#Neighborhood').val("");
}
},
error: function (data) {
alert('Error' + data);
}
});
});
With the help of Lucas Costa and Gabriel Falieri I changed from Desable to readonly, and it was partially resolved, as you can see in the code:
$("#ZipCode").change(function () {
var zipCodeValue = $(ZipCode).val();
var url = '@Url.Action("ReturnZipCode", "Ajax")';
$.ajax({
type: 'POST',
url: url,
data: { zipCode: zipCodeValue },
dataType: 'json',
success: function (data) {
document.getElementById("City").readOnly = true;
document.getElementById("State").readOnly = true;
$('#State').val(data.StateID).change();
setTimeout(setCity, 1000);
function setCity() {//insere a cidade após 3 segundos
$('#City').val(data.CityID).change();
$('#CityID').val($('#City').val());
}
if (data.AddressName != "") {
document.getElementById("Address").readOnly = true;
$('#Address').val(data.AddressName);
} else {
document.getElementById("Address").readOnly = false;
$('#Address').val("");
}
if (data.Neighborhood != "") {
document.getElementById("Neighborhood").readOnly = true;
$('#Neighborhood').val(data.Neighborhood);
} else {
document.getElementById("Neighborhood").readOnly = false;
$('#Neighborhood').val("");
}
},
error: function (data) {
alert('Error' + data);
}
});
});
But in the dropdown (City and State) still allows the change. For the select type it does not work, there is another option?
Maybe leave it as
readonly
instead ofdisabled
– BrTkCa
For select, you can use the following code to disable it:
$('#selectID').prop('disabled',true);
just change theselectID
by the ID of the select to be disabled.– Filipe Moraes