Ich habe hier den Code und Ablauf meines Projekts. Ich habe hier 3 für Kontinent eins für Land 1 für Stadt auswählen. Ich bekomme Daten, um diese aus ajax-Anfrage zu füllen Ich möchte ein paar Funktionen haben
1.Wenn Kontinent ausgewählt wird, wird die Liste der Länder für diesen Kontinent in der Länderliste aufgeführt, wenn die Änderung stattfindet. Ich möchte die Stadt um auch die Städte der ersten Einreise im Land anzuzeigen es ist nicht passiert was ich tue ist, ich muss noch den eintrag ändern In Land auswählen, um die Liste der Städte anzuzeigen
2.Frage ist, muss ich eine weitere Ajax-Anfrage in der Ajax-Anfrage für Kontinent hinzufügen? Ich bin mir nicht sicher, ob dies möglich ist. Ich habe es ausprobiert
Ajax-Code
$('.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>');
}
}
});
});
Aus der Datenbank stelle ich den Wert in die Option select like
$("#continent").val(continentid);
$("#continent").change();
$("#country").change();
$("#country").val(countryid);
$("#city").val(cityid);
Sie können ein Änderungsereignis für das Länderelement auslösen, sobald es gefüllt ist
$('.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>');
}
}
});
});
Sie können sowohl die Länderliste als auch die Stadtliste des ersten Landes in ../include/continent.php
erhalten:
type
Beispiel:
type | sysid | name
country | 1 | America
country | 2 | Canada
city | 1 | New York
city | 2 | Los Angles
Dann in 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...
}
}
});