作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的数据库由国家和城市组成。
第一个案例 - 成功完成:
第二种情况 - 无法成功:
我知道我必须使用 jQuery/Ajax。我试过了,但由于缺乏编程经验,我无法解决我的问题。我的列表是从数据库而不是 XML 中获取的。我只需要一个快速的解决方案,我需要保持简单和愚蠢。
我使用的是常规的 PHP 编码风格,而不是面向对象的。
我该怎么做?任何相关资源将不胜感激。
最佳答案
$("#country").change(function(){
$('#city').find('option').remove().end(); //clear the city ddl
var country = $(this).find("option:selected").text();
alert(country);
//do the ajax call
$.ajax({
url:'getCity.php'
type:'GET',
data:{city:country},
dataType:'json',
cache:false,
success:function(data){
data=JSON.parse(data); //no need if dataType is set to json
var ddl = document.getElementById('city');
for(var c=0;c<obj.length;c++)
{
var option = document.createElement('option');
option.value = obj[c];
option.text = obj[c];
ddl.appendChild(option);
}
},
error:function(jxhr){
alert(jxhr.responseText);
}
});
});
在你的 getCity.php 中
$country = $_GET['city'];
//do the db query here
$query = "your query";
$result = mysql_query($query);
$temp = array();
while ($row = mysql_fetch_assoc($result)) {
if(empty($temp))
{
$temp=array($row['city']);
}
else
{
array_push($temp,$row['city']);
}
}
echo (json_encode($temp));
关于php - 如何使用 jQuery 在 PHP 中制作级联下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6857287/
我是一名优秀的程序员,十分优秀!