ajax级联菜单实现方法实例分析
这篇文章主要介绍了ajax级联菜单实现方法,结合实例形式分析了基于ajax与后台php交互实现级联菜单功能的相关操作技巧,需要的朋友可以参考下
本文实例讲述了ajax级联菜单实现方法。分享给大家供大家参考,具体如下:
效果如下:
选择第一项,第二项、第三项的内容跟着改变。
选择第二项,第三项的内容跟着改变。
第三项则不影响第一项和第二项。
有几点值得提:
1.html到底是前台拼接还是后台拼接。
我选择的是前台拼接,这样可以节省流量,和后台的资源。这也比较符合程序处理,一般后台只负责提供数据。
通过json传递给前台,完了前台获取进行处理。
ajax函数
function ajaxgetbigclass(val){ $.ajax({ type:"POST", async:false, url:"/default/index/ajax/do/ajaxgetbigclass", data:"typeid="+val, success:function(response){ if(response){ res = response; }else{ res = false; } } }); return res; } function ajaxgetsmallclass(val){ $.ajax({ type:"POST", async:false, url:"/default/index/ajax/do/ajaxgetsmallclass", data:"bigclassid="+val, success:function(response){ if(response){ res = response; }else{ res = false; } } }); return res; }
后台ajax处理代码
case 'ajaxgetbigclass': $typeid = trim($this->_getParam('typeid')); $daoNews = new dao_news(); if(isset($typeid)){ $bigClass = $daoNews->getBigClassByType($typeid,true); if($bigClass){ $json = json_encode($bigClass); echo $json; }else{ echo FALSE; } }else{ echo FALSE; } break; case 'ajaxgetsmallclass': $bigclassid = trim($this->_getParam('bigclassid')); $daoNews = new dao_news(); if(isset($bigclassid)){ $smallClass = $daoNews->getSmallClassByBigClass($bigclassid,true); if($smallClass){ $json = json_encode($smallClass); echo $json; }else{ echo FALSE; } }else{ echo FALSE; } break;
调用ajax函数,并拼接成html函数
function setbigclass(id,flag){ var flag = arguments[1] ? arguments[1] : false;//默认值 var res = ajaxgetbigclass(id); //alert(res); if(res){ myobj = eval(res); for(var i=0;i<myobj.length;i++){ strHtml+="<option value='"+myobj[i].id+"'>"+myobj[i].name+"</option>"; } $("#bigclassid").html(strHtml); }else{ var strHtml = "<option value=''>无子选项</option>"; $("#bigclassid").html(strHtml); } if(flag&&res){ return myobj[0].id; } } function setsmallclass(id){ var res = ajaxgetsmallclass(id); //alert(res); if(res){ myobj = eval(res); var strHtml = "<option value=''>请选择</option>"; for(var i=0;i<myobj.length;i++){ strHtml+="<option value='"+myobj[i].id+"'>"+myobj[i].name+"</option>"; } $("#smallclassid").html(strHtml); }else{ var strHtml = "<option value=''>请选择</option><option value=''>无子选项</option>"; $("#smallclassid").html(strHtml); } }
主函数,事件动作
$(function(){ //ajax级联 $("#typeid").change(function(){ var id = $(this).val(); var res = setbigclass(id,true); if(res){ setsmallclass(res); }else{ setsmallclass(0); } }); $("#bigclassid").change(function(){ var id = $(this).val(); setsmallclass(id); }); });
2.后台查询函数化。
public function getType($where = false, $order = 'typeid ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false){ return $this->getData($this->_typename,$where,$order,$pagesize,$offset,$count,$from,$join,$group); } public function getTypeName($flag=false){ $where = array(); $aType = $this->getType($where); if($aType){ if($flag){ foreach ($aType as $key => $value) { $type[$key]['id'] = $value['typeid']; $type[$key]['name'] = $value['typename']; } return $type; }else{ foreach ($aType as $key => $value) { $type[$value['typeid']] = $value['typename']; } return $type; } }else{ return false; } } public function getBigClass($where = false, $order = 'BigClassID ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false){ return $this->getData($this->_bigname,$where,$order,$pagesize,$offset,$count,$from,$join,$group); } public function getBigClassByType($typeid = 60,$flag=false){ $where = array(); $where['BigClass.typeid =?'] = array("type"=>1,"val"=>$typeid); //print_r($where);exit; $from = array('BigClassID',"BigClassName","convert(text,BigClassMaster) as BigClassMaster","typeid"); $aBigClass = $this->getBigClass($where, false, false, false, false,$from); if($aBigClass){ if($flag){ foreach ($aBigClass as $key => $value) { $bigClass[$key]['id'] = $value['BigClassID']; $bigClass[$key]['name'] = $value['BigClassName']; } return $bigClass; }else{ foreach ($aBigClass as $key => $value) { $bigClass[$value['BigClassID']] = $value['BigClassName']; } return $bigClass; } }else{ return false; } } public function getSmallClass($where = false, $order = 'SmallClassID ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false){ return $this->getData($this->_smallname,$where,$order,$pagesize,$offset,$count,$from,$join,$group); } public function getSmallClassByBigClass($BigClassID = 221,$flag=false){ $where = array(); $where['SmallClass.BigClassID =?'] = array("type"=>1,"val"=>$BigClassID); //print_r($where);exit; $aSmallClass = $this->getSmallClass($where); if($aSmallClass){ if($flag){ foreach ($aSmallClass as $key => $value) { $smallClass[$key]['id'] = $value['SmallClassID']; $smallClass[$key]['name'] = $value['smallclassname']; } return $smallClass; }else{ foreach ($aSmallClass as $key => $value) { $smallClass[$value['SmallClassID']] = $value['smallclassname']; } return $smallClass; } }else{ return false; } }
这样就可以多处使用,多种角度使用。
3.前台js,文件化,同一个功能的js放在一个js文件中。内容最后也函数化。
<script type="text/javascript" src="/js/news/cascade.js"></script> <tr> <td width="20%" height="56" align="right" >请选择分类:</td> <td width="80%" style="padding:10px;"> <select id="typeid" name="typeid" class=" ffb-input"> <!--{html_options options=$aType selected=$aData.typeid|default:'0'}--> </select> > <select id="bigclassid" name="bigclassid" class=" ffb-input"> <!--{html_options options=$aBigClass selected=$aData.bigclassid|default:'0'}--> </select> > <select id="smallclassid" name="smallclassid" class=" ffb-input"> <option value="">请选择</option> <!--{html_options class=" ffb-input" options=$aSmallClass selected=$aData.smallclassid|default:'0'}--> </select> </td> </tr>
这样会让文件很清晰。
优化后的js
$(function(){ //ajax级联 $("#typeid").change(function(){ var id = $(this).val(); setbigclass(id); }); $("#bigclassid").change(function(){ var id = $(this).val(); setsmallclass(id); }); }); function setbigclass(id){ var res = ajaxgetbigclass(id); var strHtml; if(res){ myobj = eval(res); for(var i=0;i"+myobj[i].name+""; } $("#bigclassid").html(strHtml); $("#bigclassid").show().change(); }else{ $("#bigclassid").hide(); $("#smallclassid").hide(); } } function setsmallclass(id){ var res = ajaxgetsmallclass(id); if(res){ myobj = eval(res); var strHtml = ""; for(var i=0;i "+myobj[i].name+""; } $("#smallclassid").html(strHtml); $("#smallclassid").show(); }else{ $("#smallclassid").hide(); } } function ajaxgetbigclass(val){ $.ajax({ type:"POST", async:false, url:"/default/index/ajax/do/ajaxgetbigclass", data:"typeid="+val, success:function(response){ if(response){ res = response; }else{ res = false; } } }); return res; } function ajaxgetsmallclass(val){ $.ajax({ type:"POST", async:false, url:"/default/index/ajax/do/ajaxgetsmallclass", data:"bigclassid="+val, success:function(response){ if(response){ res = response; }else{ res = false; } } }); return res; }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

In order to improve Ajax security, there are several methods: CSRF protection: generate a token and send it to the client, add it to the server side in the request for verification. XSS protection: Use htmlspecialchars() to filter input to prevent malicious script injection. Content-Security-Policy header: Restrict the loading of malicious resources and specify the sources from which scripts and style sheets are allowed to be loaded. Validate server-side input: Validate input received from Ajax requests to prevent attackers from exploiting input vulnerabilities. Use secure Ajax libraries: Take advantage of automatic CSRF protection modules provided by libraries such as jQuery.

Ajax is not a specific version, but a technology that uses a collection of technologies to asynchronously load and update web page content. Ajax does not have a specific version number, but there are some variations or extensions of ajax: 1. jQuery AJAX; 2. Axios; 3. Fetch API; 4. JSONP; 5. XMLHttpRequest Level 2; 6. WebSockets; 7. Server-Sent Events; 8, GraphQL, etc.
