Home Backend Development PHP Tutorial 容易的ThinkPHP 二级联动

容易的ThinkPHP 二级联动

Jun 13, 2016 pm 01:18 PM
gt lt nbsp option quot

简单的ThinkPHP 二级联动

在网上看了很多关于thinkphp二级联动的文章,但我个人感觉写的都不是很简单,有些繁琐,下面是我写的一个简单的二级联动。本人是新手 高手可以不用往下看了.

html文件:

liandong.html

首先要引入jquery.js文件

二级联动

 $(function(){
        $('#AreaId').change(function(){
            //获取父类的id
            var $id=$('#AreaId').val();

           //通过ajax进行传值

            $.getJSON('__URL__/liandong',{id:$id},function(data){

                //回调函数
                if (data.status==1){ 

                   //首先清除子类中值不为空的,如果没有这句话你会发现子类的显示会这个增加,二不是你想要的结果
                    $('#ServerId option[value!=""]').remove();

                    //计算返回数组的数目,并循环显示

                    for (var i=0;i

                         //定义html标签,和显示的值,id和type_name为数据库中的字段名

                         var option ="";

                                //显示的位置
                                $(option).appendTo('#ServerId');
                    }
                }
            });
        });
    });




php文件

IndexAction.class.php

class IndexAction extends Action {

           public function liandong(){

                  //实例化数据表

                  $type=M('Type');

                  //首先是查询类别表中的父类,我的是父类的pid都为0

                  $plei=$type->where('pid=0')->select();

                   //分配变量

                   $this->assign("plei",$plei);

                   //这个主要是判断父类的内容是否发生变化,此判断非常重要(ps:我那会没写这个判断,总是出错,非常头疼)

                   if($_GET[id]){

                   $sel=$type->where('pid='.$_GET[id])->select();

                   $this->ajaxReturn($sel,'子类',1);

                  }

                 $this->display();

           }

}

这是我的类别表数据库截图

这是我将我那个项目用到二级联动的地方 ,将代码写出来 这不是我复制粘贴的,可能有字幕或标点写错的地方,还请大家仔细阅读

如有什么疑问可以给我留言

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 Article Tags

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)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

How to change title bar color on Windows 11?

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

How to enable or disable taskbar thumbnail previews on Windows 11

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

OOBELANGUAGE Error Problems in Windows 11/10 Repair

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

Display scaling guide on Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

10 Ways to Adjust Brightness on Windows 11

See all articles