Ajax实现列表无限加载和二级下拉选项效果

时间:2021-05-28

Ajax做列表无限加载和Ajax做二级下拉选项,供大家参考,具体内容如下

//栏目Ajax做加载public function ajaxlist(){ //echo "http:///index.php?a=Index&c=Index&m=ajaxlist"; //echo "<hr>"; $data = Q('sum'); $where = array(); $where['cid'] = 33; $rongyuList = M('content')->limit($data,2)->where($where)->select(); $data['stat'] = 1; $data = $rongyuList; $this->ajax($data); //也可以手动把想要的字段拼接成字符串 }

具体页面实现:

<script type='text/javascript'>(function(){//发送数据 var url = "__WEB__"+"?a=Index&c=Index&m=ajaxlist";var oSum = ''; $('a.ajaxBut').click(function(){ oSum = $('div.zizhiListContBox>a').size(); $.post(url,{sum:oSum},function(result){ console.log(result); eval("var info="+result); for(var key in info){ oStr = "<a href='"+"__WEB__"+"?a=Index&amp;c=Index&amp;m=content&amp;mid=1&amp;cid=33&amp;aid="+info[key]['aid']+""+"'><h3 class='f100 f16 ts500'>"+info[key]['title']+"</h3><p>"+info[key]['description']+"</p><span class='b parb'></span></a>"; $('div.zizhiListContBox').append(oStr); }; });});})();</script>

Ajax 做二级选项:

<!-- 示例:HTML --><dl class="pr keshi" > <dt class="pa">科室:</dt> <dd class="pa"> <select name='keshi' class='m_keshi'> <option value='0'>--请选择科室--</option> </select> <select name='zhuanjia' class='m_zhuanjia'> <option>--请选择专家--</option> </select> </dd></dl>

示例控制器:

//示例控制器public function ajaxlanmu(){ $lanmuList = M('category')->where('pid=142')->select(); $this->ajax($lanmuList);}public function ajaxzhuanjia(){ $where = array(); $data = Q('sum'); $data = $data ? $data : 143; $where['cid'] = $data; $zhuanjiaList = M('guahao')->where($where)->select(); $this->ajax($zhuanjiaList);}

示例:JS

<script>(function(){var lanmuUrl = "__WEB__"+"?a=Index&c=Index&m=ajaxlanmu";var zhuanjiaUrl = "__WEB__"+"?a=Index&c=Index&m=ajaxzhuanjia";var oSum = oStr = oStr2 = oVal = oKong = info2 = oCid = '';$.post(lanmuUrl,function(result){ eval("var info="+result); for(var key in info){oStr += "<option value='"+info[key]['catname']+"' cid='"+info[key]['cid']+"'>"+info[key]['catname']+"</option>";}; $('dl.keshi').find('select.m_keshi').append(oStr);});$('dl.keshi').find('select.m_keshi').change(function(){ oVal = $(this).find('option:selected').val(); if(oVal == 0){ $('dl.zhuanjia').find('select.m_zhuanjia').html("<option>--请选择专家--</option>"); }else{ oCid = $(this).find('option:selected').attr('cid'); $.post(zhuanjiaUrl,{sum:oCid},function(result){ eval("info2="+result); oStr2 = '';//注意这里要清空第一次请求的数据 for(var key2 in info2){ oStr2 += "<option value='"+info2[key2]['title']+"'>"+info2[key2]['title']+"</option>"; }; $('dl.zhuanjia').find('select.m_zhuanjia').html(oStr2); }); };});})();</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章