php怎么实现选择了分类,子分类才能出现对应的内容。最好是 thinkphp。谢谢

2025-01-02 05:42:08
推荐回答(1个)
回答1:

thinkphp jquery与ajax实现省市县三级联动菜单

本示例以省市区三级联动菜单为例:
实现步骤:
第一:先默认从数据库中读取所有的省份。
第二:通过jquery中的change事件,当省份的内容发生改变时,执行change事件,
通过jquery 获得被选中的省份的value值,再后通过异步加载数据。
第三:与第二步相似,当用户选择城市时,会触发change事件,通过jquery 获得被选中的城市的value值,再次进行异步加载数据。

具体示例代码如下:
RecruitmentAction.class.php
class RecruitmentAction extends Action{
public function chacity(){
$code=I('post.code');
$city=M('city')->where(array('provincecode'=>$code))->select();
echo "";
if($city){
foreach($city as $c){
echo "";
}
}
}
public function chaarea(){
$code=I('post.code');
$area=M('area')->where(array('citycode'=>$code))->select();
echo "";

if($area){
foreach($area as $a){
echo "";
}
}
}
public function chacitydefault(){
$code=I('post.code');
$city=M('city')->where(array('provincecode'=>$code))->select();
if($city){
foreach($city as $c){
echo "";
}
}
}
public function chaareadefault(){
$code=I('post.code');
$area=M('area')->where(array('citycode'=>$code))->select();
if($area){
foreach($area as $a){
echo "";
}
}
}
?>

sendrecruitment.php



省市县三级联动菜单</tilte><br><scirpt src="libs.baidu.com/jquery/1.11.1/jquery.min.js"></script><br><script><br>$(function(){<br> $('#sheng').change(function(){<br> var code=$('#sheng option:selected').val();<br> $.post('__URL__/chacity',{'code':code},function(data){<br> $('#city').html(data);<br> }) <br> }) <br> $('#city').change(function(){<br> var code=$('#city option:selected').val();<br> $.post('__URL__/chaarea',{'code':code},function(data){<br> $('#area').html(data);<br> })<br> })<br> var code=$('#sheng option').eq(0).val(); <br> $.post('__URL__/chacitydefault',{'code':code},function(data){<br> $('#city').html(data);<br> })<br> var areacode=110100;<br> $.post('__URL__/chaareadefault',{'code':areacode},function(data){<br> $('#area').html(data);<br> })<br>})<br></script><br></head><br><body><br> <?php<br> $m=M('province');<br>$p=$m->order('id asc')->select();<br> ?><br><!-- <input type="text" name="jobplace" value="" /><br>--> <select name="job_province" id="sheng" style="width:100px;"><br> <?php<br> if($p){<br> foreach($p as $v){<br> ?><br> <option value="<?php echo $v['code'];?>"><?php echo $v['name'];?></option><br> <?php<br> }<br> }<br> ?><br> </select><br> <select name="job_city" id="city" style="width:100px;"></select><br> <select name="job_area" id="area" style="width:100px;"></select><br></body><br></html><br><br>注:本示例中,控制器部分:chacitydefault(),chaareadefault()方法是为了默认页面加载时,执行一次,让省,市,与区进行显示。<br>这是我以前学习时找的的一个DEMO 你可以参考一下 <br><br>就该对你有用 <br>希望能够帮到你</p> </div> </div> <div class="clear"></div> </div> </div> </div> <div class="wendaright"> <div class="wdluluerwema"> <div class="wdxgwttop">相关问答</div> <div class="wdxgwtnr"> </div> <div class="clear"></div> </div> <!-- 其他随机问答['id'=>alphaID($like['zid'])] --> <div class="wdluluerwema"> <div class="wdxgwttop">最新问答</div> <div class="wdxgwtnr"> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/217652376.html">武汉杨春湖客运站,半个小时发一趟车到仙桃,是真的吗????谁可以告诉我肯定的答案啊???</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/1436174939262453779.html">如何提高课堂互动的技巧</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/754886525967243844.html">OPPO手机短信拦截到哪里找?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/301395313.html">能否将《致我们终将逝去的青春》新番外《听说那时你哭了》发给我,十分感谢.</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/285890646.html">四川农业大学的林学真的很差吗?以后的就业真的很难?待遇很低吗?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/607365450.html">我是星期五在富士康面试。体检完了。说星期一去培训还是干嘛。我也不清楚,但我之前有个朋友厂牌都发了,</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/302700876.html">我被上海旅游高等专科学校录取了,但查不到是哪个专业,请问怎么查询?很急,谢谢</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/268994380.html">贷款购买2手房,比如90万的房子,首付30万,剩余60万分20年还清。请问每月还款多少钱</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/2078330602005250348.html">现代名图行车过程中按一键启动熄火!后果是什么?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://www.g230.com/index.php/5600762.html">饭量挺大,为什么我吃不胖?</a></div> </div> </div> </div> </div> <div class="clear"></div> <div class="footer"> <!-- 移动底部导航 --> <div class="fanhuitop"><a href="#top" ref="nofollow"><img src="https://www.g230.com/static/old/img/fhtop.png" alt="返回顶部" title="返回顶部"></a></div> <div class="dibu"> <div class="dibu"> </div> </div> <div class="banquan"> <p>内容全部来源于网络收集,如有侵权,请联系网站删除:QQ:24596024</p> </div> </div> </div> </div> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?78733b427b0a9dce19688d903e89fc12"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>