Home Web Front-end JS Tutorial How to implement Ajax's three-level linkage product classification

How to implement Ajax's three-level linkage product classification

Apr 03, 2018 pm 05:26 PM
ajax Linkage

This time I will show you how to implement Ajax's three-level linkage product classification, and what are the precautions for implementing Ajax's three-level linkage product classification. The following is a practical case, let's take a look.

Idea analysis:

Effect: When the page is loaded, ajax is used to asynchronously request data from the background, load the first-level product category, and when the page is selected When selecting first-level products, load second-level products, and select second-level products to load third-level products.

Implementation:

1. After getting the data, load the product with pid 0, and dynamically create an option to add the product to the first level. menu, and set the value

2. When selecting a first-level product, load the product with pid=current id, and create an option to append the product to the secondary menu, and set the value Value

3. When selecting a secondary product, load the product with pid = current id, create an option to append the product to the third-level menu, and set the value

Page effect:

$(function(){
      //请求路径
      var url="03goods.php";
      //option默认内容
      var option="<option value=&#39;0&#39;>未选择</option>";
      //获取jq对象
      var $sel1=$(".sel1");
      var $sel2=$(".sel2");
      var $sel3=$(".sel3");
      //自动生成一个<option>元素
      function createOption(value,text){
        var $option=$("<option></option>");
        $option.attr("value",value);
        $option.text(text);
        return $option;
      }
      //加载数据
      function ajaxSelect($select,id){
        //get请求
        $.get(url,{"pid":id},function(data){
          $select.html(option);
          for(var k in data ){
            $select.append(createOption(data[k].id,data[k].name));
          }
        },"json");
      }
      //自动加载第一个下拉菜单
      ajaxSelect($sel1,"0");
      //选择第一个下拉菜单时加载第二个
      $sel1.change(function(){
        var id=$sel1.val();
        if(id=="0"){
          $sel2.html(option);
          $sel3.html(option);
        }else{
          ajaxSelect($sel2,id);
        }
      });
      //选择第二个下拉菜单时加载第三个
      $sel2.change(function(){
        var $id=$sel2.val();
        if($id=="0"){
          $sel3.html(option);
        }else{
          ajaxSelect($sel3,$id);
        }
      });
    });
Copy after login

Backend code:

<?php
  header(&#39;Content-Type:text/html; charset=utf-8&#39;);
  //数据
  $arr=array(
   //array(分类id,分类名,分类的父id)
   array(&#39;id&#39;=>'1','name'=>'数码产品','pid'=>'0'),
   array('id'=>'2','name'=>'家电','pid'=>'0'),
   array('id'=>'3','name'=>'书籍','pid'=>'0'),
   array('id'=>'4','name'=>'服装','pid'=>'0'),
   array('id'=>'5','name'=>'手机','pid'=>'1'),
   array('id'=>'6','name'=>'笔记本','pid'=>'1'),
   array('id'=>'7','name'=>'平板电脑','pid'=>'1'),
   array('id'=>'8','name'=>'智能手机','pid'=>'5'),
   array('id'=>'9','name'=>'功能机','pid'=>'5'),
   array('id'=>'10','name'=>'电视机','pid'=>'2'),
   array('id'=>'11','name'=>'电冰箱','pid'=>'2'),
   array('id'=>'12','name'=>'智能电视','pid'=>'10'),
   array('id'=>'13','name'=>'编程书籍','pid'=>'3'),
   array('id'=>'14','name'=>'JavaScript','pid'=>'13'),
  );
  //获取指定分类的商品
  function getByPid($arr,$pid){
    $result=array();
    foreach($arr as $v){
      if($v['pid']==$pid){
       $result[]=$v;
      }
    }
    return $result;
  }
  //获取请求参数
  $pid=isset($_GET['pid'])?$_GET['pid']:'0';
  $result=getByPid($arr,$pid);
  //输出json数据
  echo json_encode($result);
?>
Copy after login

I believe you have mastered the method after reading the case in this article, more exciting Please pay attention to other related articles on php Chinese website!

Recommended reading:

How to achieve secondary linkage with Ajax combined with PHP

The components and core of Ajax technology that novices must read Principle analysis

The above is the detailed content of How to implement Ajax's three-level linkage product classification. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zhengtu IPx classic animation 'Journey to the West' The journey to the west is fearless and fearless Zhengtu IPx classic animation 'Journey to the West' The journey to the west is fearless and fearless Jun 10, 2024 pm 06:15 PM

Journey through the vastness and set foot on the journey to the west! Today, Zhengtu IP officially announced that it will launch a cross-border cooperation with CCTV animation "Journey to the West" to jointly create a cultural feast that combines tradition and innovation! This cooperation not only marks the in-depth cooperation between the two major domestic classic brands, but also demonstrates the unremitting efforts and persistence of the Zhengtu series on the road of promoting Chinese traditional culture. Since its birth, the Zhengtu series has been loved by players for its profound cultural heritage and diversified gameplay. In terms of cultural inheritance, the Zhengtu series has always maintained respect and love for traditional Chinese culture, and skillfully integrated traditional cultural elements into the game, bringing more fun and inspiration to players. The CCTV animation "Journey to the West" is a classic that has accompanied the growth of generations.

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

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.

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

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

Double chef ecstasy! 'Onmyoji' x 'Hatsune Miku' collaboration starts on March 6 Double chef ecstasy! 'Onmyoji' x 'Hatsune Miku' collaboration starts on March 6 Feb 22, 2024 pm 06:52 PM

NetEase's "Onmyoji" mobile game announced today that the Onmyoji x Hatsune Miku limited collaboration will officially begin on March 6. The collaboration-limited SSR Hatsune Miku (CV: Saki Fujita) and SSR Kagamine Rin (CV: Asami Shimoda) are coming to Heian Kyo! The linkage online special performance event will officially start in the game on March 9~

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

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 jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

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

Fried chicken is a great business and there is no room for error! 'Backwater Cold' links up with KFC, causing players to 'dance upon hearing the chicken' Fried chicken is a great business and there is no room for error! 'Backwater Cold' links up with KFC, causing players to 'dance upon hearing the chicken' Apr 17, 2024 pm 06:34 PM

On the date, "Backwater Cold" officially announced that it will launch a linkage with KFC from April 19th to May 12th. However, the specific content of the linkage has left many people stunned. They repeatedly said, "It's embarrassing to heaven" and "It's important to society." died"! The reason lies in the slogan of this theme event. Friends who have seen the KFC linkage of "Genshin Impact" and "Beng Tie" must have the impression that "encountering another world and enjoying delicious food" has become a reality in "Ni Shui Han" Now: shout out to the clerk, "God is investigating the case, who are you?" The clerk needs to reply, "Fried chicken is a big business, and there is no room for error!" Training guide for employees: Never laugh! Not only that, this collaboration also held a dance competition. If you go to the theme store and perform the "Dance when you hear 'Ji'" dance move, you can also get a small rocking music stand. Embarrassing, so embarrassing! But that's what I want

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

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.

See all articles