答案 0 :(得分:0)
这就是我通常在php / ajax中为区域做的事情 - 省:
jQuery代码:
$('#idRegion').change(function(){
if($(this).val() == ''){
$('#idProvince').attr('disabled', 'true');
}else{
var idRegione = $(this).val();
$.getJSON('index.php',
{
option: "com_spin",
controller: "guest",
task: "getProvincieByRegionId",
idRegione: idRegione
},
function(json){
$('#idProvince').html(json.html);
}
);
$('#idProvince').removeAttr('disabled');
}
});
PHP代码:
function getProvincieByRegionId() {
Zend_Loader::loadClass ( 'Zend_Json' );
$idRegione = JRequest::getVar ( "idRegione" );
$modelProvince = new Spin_lkpprovincia ();
$provincie = $modelProvince->getElencoProvinciePerRegione ( $idRegione );
$html = "<option value=''>Selezionare una voce</option>";
foreach ( $provincie as $provincia ) {
$html .= "<option value='" . $provincia ['idProvincia'] . "'>" . $provincia ['nome'] . "</option>";
}
$json = array (
success => "OK",
html => $html );
$json = Zend_Json::encode ( $json );
echo $json;
die ();
}
您可以将此作为起点
答案 1 :(得分:0)
我不完全理解你对一个PHP文件的意思,但这是我用来动态填充子选择框的代码。基本上在选择框更改时获取ID并通过jQuery执行Ajax以获取城市的JSON响应并填充下拉列表。
我使用此代码根据您选择的国家/地区填充一个城市选择框(来自一个PHP文件):
<script type="text/javascript">// <![CDATA[
$(document).ready(function(){
$('#country').change(function(){ //any select change on the dropdown with id country trigger this code $("select[id$=cities] > option").remove(); //first of all clear select items
var country_id = $('#country').val(); // here we are taking country id of the selected one.
$.ajax({
type: "GET",
url: "home/get_cities/"+country_id, //here we are calling our user controller and get_cities method with the country_id
success: function(cities) //we're calling the response json array 'cities'
{
$.each(cities,function(id,city) //here we're doing a foeach loop round each city with id as the key and city as the value
{
var opt = $('<option />'); // here we're creating a new select option with for each city
opt.val(id);
opt.text(city);
$('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
});
}
});
});
});
// ]]>
</script>
如果您愿意,可以发布数据。在PHP中你会做这样的事情:
function get_cities($country){
$this->load->model('cities_model');
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->cities_model->get_cities($country)));
}
我在这里写了一篇文章:http://theninthnode.com/2011/01/dropdown-filtering-with-codeigniter-and-ajax/