之前每次要用地区联动基本都是用的其他人写好的js做的多级联动,但是发现最大的缺点就是地区信息太老,而且不方便更新。
今天在网上搜索到2015年最新的国家地域信息数据,就打算用数据库配合ajax来做联动,后台也可以很方便的进行数据更新
代码1(jquery代码,负责ajax数据联动前段):
<code><span>div</span><span>class</span>=<span>"form-group"</span>><span>label</span><span>for</span>=<span>"province"</span>>省份<span><span>label</span>></span><span>select</span><span>class</span>=<span>"form-control"</span><span>id</span>=<span>"province"</span><span>name</span>=<span>"province"</span>><span>option</span><span>value</span> =<span>""</span> ><span><span>option</span>></span> //volist 是thinkphp的标签,这里我用thinkphp框架写的代码,自动载入省级地区信息 <span>volist</span><span>name</span>=<span>"province"</span><span>id</span>=<span>"vo"</span>><span>option</span><span>value</span> =<span>"{$vo.class_id}"</span> >{$vo.class_name}<span><span>option</span>></span><span><span>volist</span>></span><span><span>select</span>></span><span>label</span><span>for</span>=<span>"city"</span>>城市<span><span>label</span>></span><span>select</span><span>class</span>=<span>"form-control"</span><span>id</span>=<span>"city"</span><span>name</span>=<span>"city"</span>><span><span>select</span>></span><span>label</span><span>for</span>=<span>"county"</span>>县区<span><span>label</span>></span><span>select</span><span>class</span>=<span>"form-control"</span><span>id</span>=<span>"county"</span><span>name</span>=<span>"county"</span>><span><span>select</span>></span><span><span>div</span>></span><span>script</span>><span> $(document).ready(<span><span>function</span><span>()</span>{</span><span>//省数据变动</span> $(<span>"#province"</span>).change(<span><span>function</span><span>()</span>{</span> $.post(<span>'/Home/Creat/ajax'</span>, {province:$(<span>"#province"</span>).val()}, <span><span>function</span><span>(data)</span>{</span><span>//alert('成功post回数据');</span><span>if</span>(data.info == <span>'ok'</span>) { <span>//返回ok,将市下拉框赋值</span><span>//alert(data.content);</span> col_clear(<span>'city'</span>); col_add(<span>'city'</span>,data.content); $(<span>"#city"</span>).change(); } <span>else</span> { alert(data.info); } }, <span>'json'</span>).error(<span><span>function</span><span>()</span>{</span> alert(<span>"网络连接错误,请稍后再试"</span>); }); }); <span>//市数据变动</span> $(<span>"#city"</span>).change(<span><span>function</span><span>()</span>{</span> $.post(<span>'/Home/Creat/ajax'</span>, {city:$(<span>"#city"</span>).val()}, <span><span>function</span><span>(data)</span>{</span><span>if</span>(data.info == <span>'ok'</span>) { col_clear(<span>'county'</span>); col_add(<span>'county'</span>,data.content); } <span>else</span> { alert(data.info); } }, <span>'json'</span>).error(<span><span>function</span><span>()</span>{</span> alert(<span>"网络连接错误,请稍后再试"</span>); }); }); }); <span>//select操作方法</span><span>// 添加 </span><span><span>function</span><span>col_add</span><span>(id,data)</span> {</span><span>var</span> selObj = $(<span>"#"</span>+id); <span>for</span> (<span>var</span> i=<span>0</span>;i<data.length selobj.append>"<option value='"</span>+data[i].class_id+<span>"'>"</option></data.length></span>+data[i].class_name+<span>""</span>); } } <span>// 删除 </span><span><span>function</span><span>col_delete</span><span>()</span> {</span><span>var</span> selOpt = $(<span>"#mySelect option:selected"</span>); selOpt.remove(); } <span>// 清空 </span><span><span>function</span><span>col_clear</span><span>(id)</span> {</span><span>var</span> selOpt = $(<span>"#"</span>+id+<span>" option"</span>); selOpt.remove(); } <span><span>script</span>></span></code>
代码2(php后端数据判断交互代码,基于thinkphp写的,如果是其他框架,原理也是一样的,代码会有些小差入):
<code><span>public</span><span><span>function</span><span>ajax</span><span>()</span>{</span><span>$city</span> = M(<span>'city'</span>); <span>if</span>(!IS_AJAX){ <span>$this</span>->ajaxReturn(<span>array</span>( <span>'info'</span> => <span>'非法的请求方式'</span> )); } <span>//如果提交过来的是省份改变</span><span>if</span>(I(<span>'post.province'</span>,<span>0</span>,<span>'intval'</span>)){ <span>$filter</span> = <span>array</span>( <span>'class_parent_id'</span> => I(<span>'post.province'</span>,<span>0</span>,<span>'intval'</span>), <span>'class_type'</span> => <span>2</span> ); <span>$data</span>[<span>'content'</span>] = <span>$city</span>->where(<span>$filter</span>)->select(); <span>$data</span>[<span>'info'</span>] = <span>'ok'</span>; } <span>//如果提交过来的是城市改变</span><span>if</span>(I(<span>'post.city'</span>,<span>0</span>,<span>'intval'</span>)){ <span>$filter</span> = <span>array</span>( <span>'class_parent_id'</span> => I(<span>'post.city'</span>,<span>0</span>,<span>'intval'</span>), <span>'class_type'</span> => <span>3</span> ); <span>$data</span>[<span>'content'</span>] = <span>$city</span>->where(<span>$filter</span>)->select(); <span>$data</span>[<span>'info'</span>] = <span>'ok'</span>; } <span>$this</span>->ajaxReturn(<span>$data</span>); }</code>
最后是数据库,贡献下自己的数据库,根据2015年1月国家公布的地区信息写的,是最新的。
sql数据库下载
以上就介绍了基于数据库、php、ajax的 3级地区联动,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。