Lập trinh đia chỉ 3 cấp Tinh thành - Quận huyen - Phường xã
Link tải code ở dưới nha
tinhthanh.js
function GetQuanHuyenTheoTinh(codeTinh) {
$.ajax({
type: "get",
url: "/Public/DiaChi/quanhuyen.json",
dataType: "json",
success: function (response) {
console.log('re', response);
var htmlHuyen = `<option value="" ></option>`;
$.map(response, function (elementOrValue, indexOrKey) {
if (elementOrValue.parent_code == codeTinh) {
htmlHuyen += `<option value="${elementOrValue.code}" >${elementOrValue.name_with_type}</option>`;
}
});
$("#quanhuyen").html(htmlHuyen);
}
});
}
function GetPhuongXaTheoQuanHuyen(codeHuyen) {
$.ajax({
type: "get",
url: "/Public/DiaChi/phuongxa.json",
dataType: "json",
success: function (response) {
var htmlPhuong = `<option value="" ></option>`;
$.map(response, function (elementOrValue, indexOrKey) {
if (elementOrValue.parent_code == codeHuyen) {
htmlPhuong += `<option value="${elementOrValue.code}" >${elementOrValue.name_with_type}</option>`;
}
});
$("#phuongxa").html(htmlPhuong);
}
});
}
$(document).ready(function () {
$.ajax({
type: "get",
url: "/Public/DiaChi/tinhthanh.json",
dataType: "json",
success: function (response) {
console.log('re', response);
$("#tinhthanh").append(
`<option value="" >Chọn tỉnh thành phố</option>`
);
$.map(response, function (elementOrValue, indexOrKey) {
$("#tinhthanh").append(
`<option value="${elementOrValue.code}" >${elementOrValue.name_with_type}</option>`
);
});
}
});
$("#tinhthanh").change(function (e) {
GetQuanHuyenTheoTinh($(this).val());
});
$("#quanhuyen").change(function (e) {
GetPhuongXaTheoQuanHuyen($(this).val());
});
});
Link tải code
https://drive.google.com/file/d/1hbrnWuwkh-VST9iyBeaY_SVtZnYB4MQx/view?usp=drive_link