提问人:Mustafa Ozbalci 提问时间:8/22/2023 更新时间:8/22/2023 访问量:23
成功时使用 ajax 触发更改选择
Change Select using ajax triggered on success
问:
我的模式允许我更改所选分支的数据。使用 ajax,我可以通过我的 getter/get_location_info.php 获取数据,当我更改选择时,会填充以下选择。
我的城市选择是从我用 php 获取的列表中填充的,但其余的选择是通过我的 getter 用 ajax 填充的。当我选择要更新的分支时,我的模态城市选择是正确的,并且填充了县选择,它没有被选中,并且没有填充地区、宿舍。
“我要填充的脚本”选择:
<script>
$(document).ready(function() {
$('#edit_city').change(function() {
var cityId = $(this).val();
$.ajax({
type: 'POST',
url: 'functions/getters/get_location_info.php',
data: {
city_id: cityId
},
success: function(counties) {
$('#edit_county').html(counties);
$('#edit_district').html('<option value="0">Semt Seçiniz</option>');
}
});
});
$('#edit_county').change(function() {
var countyId = $(this).val();
$.ajax({
type: 'POST',
url: 'functions/getters/get_location_info.php',
data: {
county_id: countyId
},
success: function(districts) {
$('#edit_district').html(districts);
$('#edit_quarter').html('<option value="0">Köy Mahalle Seçiniz</option>');
}
});
});
$('#edit_district').change(function() {
var districtId = $(this).val();
$.ajax({
type: 'POST',
url: 'functions/getters/get_location_info.php',
data: {
district_id: districtId
},
success: function(quarters) {
$('#edit_quarter').html(quarters);
}
});
});
});
</script>
我的脚本,用于获取分支数据并更新模态输入值:
<script>
$('.btn-edit-branch').click(function() {
var branchId = $(this).data('branch_id');
var city_id, county_id, district_id, quarter_id;
$.ajax({
url: 'functions/getters/get_location_info.php',
type: 'POST',
data: {
branch_id_edit: branchId
},
success: function(data) {
var branchInfo = JSON.parse(data);
city_id = branchInfo.ref_city;
county_id = branchInfo.ref_county;
district_id = branchInfo.ref_district;
quarter_id = branchInfo.ref_quarter;
// Set branch information in modal form
$('#edit_branch_id').val(branchInfo.id);
$('#edit_branch_name').val(branchInfo.branch_name);
$('#edit_address').val(branchInfo.address);
$('#edit_branch_mail').val(branchInfo.email);
$('#edit_branch_phone').val(branchInfo.phone);
$('#edit_city').val(city_id).trigger('change');
$('#edit_county').val(county_id).trigger('change');
$('#edit_district').val(district_id).trigger('change');
$('#edit_quarter').val(quarter_id).trigger('change');
},
error: function() {
alert('Veriler çekilirken bir hata oluştu');
}
});
});
</script>
谢谢你,感谢你的帮助
答: 暂无答案
评论
.trigger('change')