Home > Article > Web Front-end > Ajax realizes infinite loading of lists and secondary drop-down option effects
This article mainly introduces Ajax to achieve infinite loading of lists and the effect of secondary drop-down options in detail. It has certain reference value. Interested friends can refer to
Ajax to achieve unlimited loading of lists. Make secondary drop-down options with Ajax for your reference. The specific content is as follows
//栏目Ajax做加载 public function ajaxlist(){ //echo "http://www.域名.com/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); //也可以手动把想要的字段拼接成字符串 /*echo "["; foreach($rongyuList as $k){ echo "{"."\""."title"."\"".":"."\"".$k['title']."\"".","."\""."description"."\"".":"."\"".$k['description']."\"".","."\""."cid"."\"".":"."\"".$k['cid']."\""."}".","; } echo "]";*/ }
Specific page implementation:
<script type='text/javascript'> /*ajax*/ (function(){ //发送数据 var url = "__WEB__"+"?a=Index&c=Index&m=ajaxlist"; var oSum = ''; $('a.ajaxBut').click(function(){ oSum = $('p.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&c=Index&m=content&mid=1&cid=33&aid="+info[key]['aid']+""+"'><h3 class='f100 f16 ts500'>"+info[key]['title']+"</h3><p>"+info[key]['description']+"</p><span class='b parb'></span></a>"; $('p.zizhiListContBox').append(oStr); }; }); }); })(); </script>
Ajax makes secondary options:
<!-- 示例: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>
Example controller:
//示例控制器 /* Ajax请求栏目列表 */ 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); }
Example: 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 = ''; /* lanmu */ $.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); }); /* zhuanjia */ $('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>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
django via ajax Methods for completing email user registration and activating accounts
Detailed explanation of the use of get and post methods of nativeajax
The above is the detailed content of Ajax realizes infinite loading of lists and secondary drop-down option effects. For more information, please follow other related articles on the PHP Chinese website!