之前每次要用地區聯動基本上都是用的其他人寫好的js做的多級聯動,但是發現最大的缺點就是地區資訊太老,而且不方便更新。
今天在網路上搜尋到2015年最新的國家地域資訊數據,就打算用資料庫配合ajax來做聯動,後台也可以很方便的進行數據更新
代碼1(jquery代碼,負責ajax數據聯動前段):
<code><span><<span>div</span><span>class</span>=<span>"form-group"</span>></span><span><<span>label</span><span>for</span>=<span>"province"</span>></span>省份<span></<span>label</span>></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><span><<span>option</span><span>value</span> =<span>""</span> ></span><span></<span>option</span>></span> //volist 是thinkphp的标签,这里我用thinkphp框架写的代码,自动载入省级地区信息 <span><<span>volist</span><span>name</span>=<span>"province"</span><span>id</span>=<span>"vo"</span>></span><span><<span>option</span><span>value</span> =<span>"{$vo.class_id}"</span> ></span>{$vo.class_name}<span></<span>option</span>></span><span></<span>volist</span>></span><span></<span>select</span>></span><span><<span>label</span><span>for</span>=<span>"city"</span>></span>城市<span></<span>label</span>></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></<span>select</span>></span><span><<span>label</span><span>for</span>=<span>"county"</span>></span>县区<span></<span>label</span>></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></<span>select</span>></span><span></<span>div</span>></span><span><<span>script</span>></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;i++) { selObj.append(<span>"<option value='"</span>+data[i].class_id+<span>"'>"</span>+data[i].class_name+<span>"</option>"</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教程有興趣的朋友有所幫助。