select 选项在更改时从 ajax 生成值
select option generate value from ajax on change
我有 我的项目的代码和流程 我有 3 select 这里一个用于大陆 一个用于国家 一个用于城市 我从 select 获取数据来填充这些 select =27=] 请求它现在工作正常我只是想有点花哨所以我想要一些功能
1.When 大陆是 select 当发生变化时,该大陆的国家/地区列表列在国家/地区列表中我想要城市
还显示当前该国第一个条目的城市
它没有发生我做的是我仍然需要更改条目
在国家 select 中显示城市列表
2.Question 我是否需要在大陆的 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>');
}
}
});
});
我从数据库中将值放入选项 select 中,例如
$("#continent").val(continentid);
$("#continent").change();
$("#country").change();
$("#country").val(countryid);
$("#city").val(cityid);
填充国家/地区元素后,您可以触发更改事件
$('.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>');
}
}
});
});
您可以获得../include/continent.php
中第一个国家的国家列表和城市列表:
- 获取国家列表数组
- 获取城市列表 where countryid = country[0].id
- 合并它们并添加另一个字段,例如
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...
}
}
});
我有
1.When 大陆是 select 当发生变化时,该大陆的国家/地区列表列在国家/地区列表中我想要城市 还显示当前该国第一个条目的城市 它没有发生我做的是我仍然需要更改条目 在国家 select 中显示城市列表
2.Question 我是否需要在大陆的 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>');
}
}
});
});
我从数据库中将值放入选项 select 中,例如
$("#continent").val(continentid);
$("#continent").change();
$("#country").change();
$("#country").val(countryid);
$("#city").val(cityid);
填充国家/地区元素后,您可以触发更改事件
$('.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>');
}
}
});
});
您可以获得../include/continent.php
中第一个国家的国家列表和城市列表:
- 获取国家列表数组
- 获取城市列表 where countryid = country[0].id
- 合并它们并添加另一个字段,例如
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...
}
}
});