Thinkphp实现省市区三级联动

一张表实现省市区三级联动【3409条数据】

1 . [PHP]代码

public function index(){
    $province = M('Tree')->where ( array('pid'=>1) )->select ();
    $this->assign('province',$province);
    $this->display();

}
public function getRegion(){
    $Region=M("Tree");
    $map['pid']=$_REQUEST["pid"];
    $map['type']=$_REQUEST["type"];
    $list=$Region->where($map)->select();
    echo json_encode($list);
}

2 .[HTML]代码

<select name="province" id="province" onchange="loadRegion('province',2,'city','{:U('Index/getRegion')}');">
   <option value="0" selected>省份/直辖市</option><volist name="province" id="vo"> 
   <option value="{$vo.id}" >{$vo.name}</option></volist>
</select>          
<select name="city" id="city"  onchange="loadRegion('city',3,'town','{:U('Index/getRegion')}');">
  <option value="0">市/县</option>
</select>
<select name="town" id="town">
  <option value="0">镇/区</option>
</select>

3 . [JavaScript]代码

function loadRegion(sel,type_id,selName,url){
    jQuery("#"+selName+" option").each(function(){
        jQuery(this).remove();
    });
    jQuery("<option value=0>请选择</option>").appendTo(jQuery("#"+selName));
    if(jQuery("#"+sel).val()==0){
        return;
    }
    jQuery.getJSON(url,{pid:jQuery("#"+sel).val(),type:type_id},
        function(data){
            if(data){
                jQuery.each(data,function(idx,item){
                    jQuery("<option value="+item.id+">"+item.name+"</option>").appendTo(jQuery("#"+selName));
                });
            }else{
                jQuery("<option value='0'>请选择</option>").appendTo(jQuery("#"+selName));
            }
        }
    );
}
</script>

4 . [SQL]代码

DROP TABLE IF EXISTS `tp_tree`;
CREATE TABLE `tp_tree` (
  `id` int(5) unsigned NOT NULL AUTO_INCREMENT,
  `pid` int(5) unsigned NOT NULL DEFAULT '0',
  `name` varchar(120) DEFAULT NULL,
  `type` tinyint(1) DEFAULT '2',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3410 DEFAULT CHARSET=utf8;

5 . TP_tree.sql ~ 215KB

链接:http://pan.baidu.com/s/1slJBhNJ 密码:wnfi

6 . 另外一个例子:

http://vote.wucheshuedu.com/index.php?s=/Home/Article/detail/id/35.html

PHP代码 <?php namespace Admin\Controller; use Think\Controller; class GoodController extends Controller { public function release() { /* * 开始新的操作 * */ $data=array(); $data['productname']=$_POST['productname']; $data['introduction']=$_POST['introduction']; $data['codenumber']=$_POST['codenumber']; $data['Productunit']=I('post.Productunit'); $data['sku']=I('post.sku'); $data['province']=I('post.province'); $data['city']=I('post.city'); $data['town']=I('post.town'); $data['classification']=I('post.classification'); $data['Extendedclass']=I('post.Extendedclass'); $data['Commoditybrand']=I('post.Commoditybrand'); $data['Commoditybrand']=I('post.Commoditybrand'); $data['Shopprice']=I('post.Shopprice'); $data['Marketvalue']=I('post.Marketvalue'); $data['Capitalizedcost']=I('post.Capitalizedcost'); $data['Commission']=I('post.Commission'); $data['original_img']=I('post.original_img'); /*商品重量*/ $data['Commodityweight']=I('post.Commodityweight'); $data['mail']=I('post.mail'); /*库存数量*/ $data['Inventoryquantity']=I('post.Inventoryquantity'); /*赠送积分*/ $data['Giftpoints']=I('post.Giftpoints'); /*兑换积分*/ $data['exchange_integral']=I('post.exchange_integral'); /*商品关检词*/ $data['Keyword']=I('post.Keyword'); /*商品详情描述*/ $data['describe']=I('post.describe'); $commodity=M('commodity'); /*准备开启事物*/ $commodity->startTrans(); $result=$commodity->add($data); if(!$result){ /*回滚事物*/ $commodity->rollback(); }else{ /*提交事物*/ $commodity->commit(); } /*1导入thinkphp得我自带的auth类库 * 加锁操作。若果同个用户(判断同个用户名操作)同时加入同样的sku时, * 那么如果存在时就加不上去(数量),如果不存在时(如果不存在时的数量不一样时)就insert上去 * */ $province = M('prvices')->where ( array('pid'=>1) )->select (); $this->assign('province',$province); $this->display(); } public function getRegion(){ /* * 接收市区 * */ $Region=M("prvices"); $map['pid']=$_REQUEST["pid"]; $map['type']=$_REQUEST["type"]; $list=$Region->where($map)->select(); echo json_encode($list); } public function sku() { $this->display(); } } html: <tr> <td>商品所在地</td> <td> <select name="addres" id="province" <option value="0" selected>省份/直辖市</option> <volist name="province" id="vo"> <option value="{$vo.id}" >{$vo.name} </option> </volist> </select> <select name="city" id="city" <option value="0">市/县</option> </select> <select name="town" id="town"> <option value="0">镇/区</option> </select> </td> </tr> function loadRegion(sel,type_id,selName,url){ jQuery("#"+selName+" option").each(function(){ jQuery(this).remove(); }); jQuery("<option value=0>请选择</option>").appendTo(jQuery("#"+selName)); if(jQuery("#"+sel).val()==0){ return; } jQuery.getJSON(url,{pid:jQuery("#"+sel).val(),type:type_id}, function(data){ if(data){ jQuery.each(data,function(idx,item){ jQuery("<option value="+item.id+">"+item.name+"</option>").appendTo(jQuery("#"+selName)); }); }else{ jQuery("<option value='0'>请选择</option>").appendTo(jQuery("#"+selName)); } } ); }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值