我是Jquery和Yii框架的新手。这个问题我有个问题。
表:
有两个表
CITY: city_id
, name
DISTRICT: dis_id, city_id, name (city_id is foreign key of CITY(city_id))
任何人都可以帮助我如何创建2组合框,当我选择城市名称同时它显示地区。有代码吗?
提前感谢。
对不起,我的英语不好。
试着读一下,
//你的Jquery Ajax应该是这样的
$("#firstcomboboxid").change(function() { //when your first combobox made changes
$.post( "/controllername/functionname", $( "#yourform" ).serialize(), function(data){
//Response from server after query
if(data.result == 'success'){
$("#secondcomboboxid").empty(); //Make sure the combobox is empty
$.each(data.district,function(i,val)){
$("#secondcomboboxid").html("<option value='"+i+"'>"+val+"</option>"); //bind every of them into combobox
}
}
},'json');
}
//你的控制器{当前的controllername}
public function functionname(){ //functionname to be replace
/* Starts your query here */
$result = .....; //For example we will use $result as variable and assuming it will be an array as result.
/* End of your query */
exit(json_encode(array('result' => 'success','district' => $result))); //Return with JSON encode
}
希望这对你有帮助。谢谢。