我有here我的项目的代码和流程我有3个选择这里一个大陆一个国家一个城市我得到数据填充这些选择从ajax请求它现在工作正常我只是想有点花哨所以我想要一些功能
1.当选择Continent时,当发生更改时,该国家/地区列表会列在国家/地区列表中我希望城市 还显示目前该国首个入境城市 它没有发生我所做的是我仍然需要更改条目 在国家/地区选择显示城市列表
2.问题是我需要在大陆的ajax请求中添加另一个ajax请求我不确定这个是否可行我试过了, 它现在不起作用
Ajax代码
$('.continentname').change(function() {
var id = $(this).find(':selected')[0].id;
//alert(id);
$.ajax({
type:'POST',
url:'../include/continent.php',
data:{'id':id},
success:function(data){
// the next thing you want to do
var country= document.getElementById('country');
$(country).empty();
var city = document.getElementById('city');
$(city).empty();
for (var i = 0; i < data.length; i++) {
$(country).append('<option id=' + data[i].sysid + ' value=' + data[i].name + '>' + data[i].name + '</option>');
}
}
});
});
$('.countryname').change(function() {
var id = $(this).find(':selected')[0].id;
$.ajax({
type:'POST',
url:'../include/country.php',
data:{'id':id},
success:function(data){
// the next thing you want to do
var city = document.getElementById('city');
$(city).empty();
for (var i = 0; i < data.length; i++) {
$(city).append('<option id=' + data[i].sysid + ' value=' + data[i].name + '>' + data[i].name + '</option>');
}
}
});
});
从数据库中我将值放入选项中选择
$("#continent").val(continentid);
$("#continent").change();
$("#country").change();
$("#country").val(countryid);
$("#city").val(cityid);
答案 0 :(得分:7)
填充后,您可以触发国家/地区元素的更改事件
$('.continentname').change(function () {
var id = $(this).find(':selected')[0].id;
//alert(id);
$.ajax({
type: 'POST',
url: '../include/continent.php',
data: {
'id': id
},
success: function (data) {
// the next thing you want to do
var $country = $('#country');
$country.empty();
$('#city').empty();
for (var i = 0; i < data.length; i++) {
$country.append('<option id=' + data[i].sysid + ' value=' + data[i].name + '>' + data[i].name + '</option>');
}
//manually trigger a change event for the contry so that the change handler will get triggered
$country.change();
}
});
});
$('.countryname').change(function () {
var id = $(this).find(':selected')[0].id;
$.ajax({
type: 'POST',
url: '../include/country.php',
data: {
'id': id
},
success: function (data) {
// the next thing you want to do
var $city = $('#city');
$city.empty();
for (var i = 0; i < data.length; i++) {
$city.append('<option id=' + data[i].sysid + ' value=' + data[i].name + '>' + data[i].name + '</option>');
}
}
});
});
答案 1 :(得分:0)
您可以在../include/continent.php
中获取第一个国家/地区的国家/地区列表和城市列表:
type
示例:
type | sysid | name
country | 1 | America
country | 2 | Canada
city | 1 | New York
city | 2 | Los Angles
然后在javascript:
$.post("../include/continet.php", {"id":id}, function(data){
$(country).empty();
$(city).empty();
for (var i = 0; i < data.length; i++) {
if(data[i].type == "country"){
$(country).append...
}
else{
$(city).append...
}
}
});