thinkPHP中省市级联下拉列表

公共函数放置位置common文件夹下common.php文件(此段代码也可放置在要使用的控制器中)

封装的下拉列表函数代码:

/**
     * 根据列表拼装成一个下拉列表 ADD BY CK
     * @param $list :数据源
     * @param $value :显示的option的value值,下标 例:$list['id']中的id
     * @param $text :显示的option的text值 例:$list['name']中的name
     * @param int $selectIndex : 选中的索引项
     * @param bool $haveFirst:是否有第一项如'<option value="0">请选择</option>',默认为false
     * @return bool|string
     */
    function getSelectOption($list,$value,$text,$selectIndex = 0,$haveFirst=false)
    {   if(!is_array($list) )
    {
        return false;
    }
        if(!isset($list)||!isset($value)||!isset($text))
        {
            return false;
        }
        if(empty($list)||empty($value)||empty($text))
        {
            return false;
        }
        if(!$haveFirst)//如果有第一项则不添加
        {
            $resultStr='<option value="0">请选择</option>';
        }
        foreach($list as $option)
        {
            if($option[$value] == $selectIndex ){
                $resultStr .= '<option value='.$option[$value].' selected="selected">'.$option[$text].'</option>';
            }else
            {
                $resultStr .= '<option value='.$option[$value].'>'.$option[$text].'</option>';
            }
        }
        return $resultStr;
    }

Exchange控制器代码:

public function index(){
$this->assign('option', array('prov' => $this->getRegion(0))); //页面初始化获取省份。
$this->display();
}
/**
     * 获取省市
     * @param type $pid
     * @param type $ajax 是否ajax调用
     * @return type array
     */
    public function getRegion($pid = 0, $ajax = false, $checked = 0, $prvcheck = false) {
        $r = D("Region")->where("parentid=" . $pid)->select();  //Region省市级联表,parentid父亲级id,pid自增id
        if ($ajax) {
            $this->ajaxReturn(getSelectOption($r, 'id', 'name'));
        } else {
            if ($prvcheck) {
                $prvid = D("Region")->query("SELECT id from hengtu_region where id=(SELECT parentid from hengtu_region where parentid=(SELECT parentid from hengtu_region where id=" . $pid . ") limit 1)");
                return getSelectOption(D("Region")->where("parentid=0")->select(), 'id', 'name', $prvid[0]['id']);
            }
            return getSelectOption($r, 'id', 'name', $checked);
        }
    }

在index页面要显示省市级联的位置:

<font style="font-size:12px;">地区选择:</font>
      <select id="addarea_sheng" name='cityid';>
      {$option.prov}
      </select>
      <select id="addarea_shi" name='cityid'  style="display:none;" >
        
      </select>
      <select id="addarea_quxian" name='cityid' style="display:none;" >
                     
      </select>
 <script>
       $("#addarea_sheng").change(function() {
            var provid = $("#addarea_sheng").val();
            $.get("{:U('Exchange/getRegion')}?pid=" + provid + "&ajax=1", function(data) {
                $("#addarea_shi").show(0, function() {
                      $(this).html(data);
                 });
            })
       })

       $("#addarea_shi").change(function() {
              var provid = $("#addarea_shi").val();
              $.get("{:U('Exchange/getRegion')}?pid=" + provid + "&ajax=1", function(data) {
                    $("#addarea_quxian").show(0, function() {
                           $(this).html(data);
                    });
              })
        })
</script>
原文地址:https://www.cnblogs.com/binggozhou/p/4282360.html