首頁 >php教程 >php手册 >ThinkPHP 三級連動

ThinkPHP 三級連動

PHP中文网
PHP中文网原創
2016-08-26 10:12:51832瀏覽

HTML:

<div id="cBox">
<select id="pro">
<option>--请选择省份--</option>
</select>
</div>

JS:

$(function(){
$.ajax({type:&#39;GET&#39;, url:&#39;__URL__/linkage&#39;, data:&#39;upid=0&#39;,
success:function( data ){
var proOption = &#39;&#39;;//用来存放省份的选项
for( var i=0;i<data.length;i++){
proOption += &#39;<option value=&#39;+data[i].id+&#39;>&#39;+data[i].name+&#39;</option>&#39;;
}
$(&#39;#pro&#39;).append(proOption);
}, dataType:&#39;json&#39;
});
$(&#39;#cBox&#39;).on(&#39;change&#39;,&#39;select&#39;,function(){
var ob = $(this);
ob.nextAll(&#39;select&#39;).remove();
var val = $(this).val();
$.get(&#39;__URL__/linkage&#39;, {upid:val}, function(res){
if( res.length <=0 ) return false;
var len = $(&#39;#cBox select&#39;).length;

switch( len ){
case 1:
idValue = &#39;city&#39;;
break;
case 2:
idValue = &#39;qu&#39;;
break;
case 3:
idValue = &#39;xian&#39;;
break;
case 4:
idValue =&#39;dao&#39;;
break;
default:
idValue = &#39;222&#39;;
break;
}
var select = &#39;<select id="&#39;+idValue+&#39;"><option>--请选择--</option>&#39;;
for( var i=0;i<res.length;i++){
select += &#39;<option value="&#39;+res[i].id+&#39;">&#39;+res[i].name+&#39;</option>&#39;;
}
select +=&#39;</select>&#39;;
ob.after( select );
},&#39;json&#39;
);
});
});

 

TP:

public function linkage() {
$map[&#39;pid&#39;] = I(&#39;get.upid&#39;);
$cityList = M(&#39;category&#39;)->where($map)->select();
echo json_encode($cityList);
}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn