I am trying to do the following: the user types his / her zip, and then it is populated automatically, estado
(select) cidade
(select), rua
(input) and bairro
(input), when the user types his / her zip and for example is a complete zip (City, State, Street and Neighborhood) the fields were disabled for editing using Disable
. When the zip is only from City (City and State) the Neighborhood and Street fields are enabled to type. For this I 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 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 the dropdown (City and State) is still allowing the change. For select type it does not work, is there another option?