公共函数放置位置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>

thinkPHP中省市级联下拉列表的更多相关文章

  1. javascript中的省市级联效果

    学习javascript的时候都遇到过这样的需求,不仅是省市,还有其他的一些场景,看看关键的代码有哪些吧. <head runat="server"> <titl ...

  2. 省市级联.net

    初学javascript,编译省市级联,使用json在一般处理程序中编译,利用ajax传递数据到web前台 <html xmlns="http://www.w3.org/1999/xh ...

  3. JavaScript实现省市级联效果实例

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. 转:『代码』JS封装 Ajax级联下拉列表

    在博客园看到一篇不错的AJAX级联下拉列表,觉得不错,特地拿下来 :转载来自:『大雪无痕』 ,原文地址 //当一个 下拉列表 改变时,触发所有联动:(警告:各下拉列表之间 请不要出现 循环依赖) // ...

  5. 全国省市级联数据sql语句 mysql版

    全国省市级联数据sql语句 mysql版 --省级 provincial create table provincial ( provincialID int, provincialName ), p ...

  6. 原生js实现级联下拉列表

    <!DOCTYPE> <html> <head> <title>级联下拉列表</title> <meta charset=" ...

  7. js省市级联实现

    js省市级联实现. for [element] in [collection] 用于循环下标,常用于json for(index in arr){ console.info("下标:&quo ...

  8. 纯js实现省市级联效果

    我们都知道一般有注册的时候会让用户填入省市啊地区什么的,然后我就想使用纯js制作一个省市级联的效果,只是用于学习以及回顾温习用,首先看下效果图,界面很丑啊,不要嫌弃! 首先还是先看下我的项目目录吧 很 ...

  9. 使用Vue-TreeSelect组件实现公司-部门-人员级联下拉列表的处理

    最近在改造原有Bootstrap开发框架,增加一个Vue&Element前端的时候,发现需要处理一个级联更新的过程,就是选择公司,然后更新部门,选择部门,或者人员列表,选择作为主管的一个实现, ...

随机推荐

  1. fgets和scanf的区别

    fgets和scanf的区别 1.测试使用scanf的一个例子: #include "stdio.h" #include "string.h" int main ...

  2. codeforces Ilya and Matrix

    http://codeforces.com/contest/313/problem/C #include <cstdio> #include <cstring> #includ ...

  3. Cracking the coding interview--Q1.1

    原文: Implement an algorithm to determine if a string has all unique characters. What if you can not u ...

  4. op+3g

    https://forum.openwrt.org/viewtopic.php?id=44895 http://eko.one.pl/forum/viewtopic.php?id=10269 http ...

  5. vs2008包加载失败

    由于安装vs2008sp1后,新建项目报错,解决未遂,于是重装vs2008,装完后又出现包加载失败问题: Microsoft.Data.Entity.Design.Package.MicrosoftD ...

  6. bzoj1188

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1188 一道非常好的SG函数题,加深了对博弈论的理解. 以前做的SG函数的题,都是每个瓶子看成一 ...

  7. URAL 2038 Minimum Vertex Cover

    2038. Minimum Vertex Cover Time limit: 1.0 secondMemory limit: 64 MB A vertex cover of a graph is a ...

  8. WustOJ 1575 Gingers and Mints(快速幂 + dfs )

    1575: Gingers and Mints Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 24 ...

  9. 如何改变Myeclipse编辑区背景色

    编辑窗口右键单击——>Preferences——>General加号——>Editors加号——>点Text Editors字样——>右下窗口选Backgroud col ...

  10. #python基础学习模块:marshal 对象的序列化

    #标准库地址:https://docs.python.org/2/library/marshal.html"""有时候,要把内存中一个对象持久化保存磁盘或者序列化二进制流 ...