ThinkPHP 三级联动
程序员文章站
2022-06-04 13:09:54
...
HTML:
<div id="cBox"> <select id="pro"> <option>--请选择省份--</option> </select> </div>
JS:
$(function(){ $.ajax({type:'GET', url:'__URL__/linkage', data:'upid=0', success:function( data ){ var proOption = '';//用来存放省份的选项 for( var i=0;i<data.length;i++){ proOption += '<option value='+data[i].id+'>'+data[i].name+'</option>'; } $('#pro').append(proOption); }, dataType:'json' }); $('#cBox').on('change','select',function(){ var ob = $(this); ob.nextAll('select').remove(); var val = $(this).val(); $.get('__URL__/linkage', {upid:val}, function(res){ if( res.length <=0 ) return false; var len = $('#cBox select').length; switch( len ){ case 1: idValue = 'city'; break; case 2: idValue = 'qu'; break; case 3: idValue = 'xian'; break; case 4: idValue ='dao'; break; default: idValue = '222'; break; } var select = '<select id="'+idValue+'"><option>--请选择--</option>'; for( var i=0;i<res.length;i++){ select += '<option value="'+res[i].id+'">'+res[i].name+'</option>'; } select +='</select>'; ob.after( select ); },'json' ); }); });
TP:
public function linkage() { $map['pid'] = I('get.upid'); $cityList = M('category')->where($map)->select(); echo json_encode($cityList); }