ajax二级联动菜单源码,二级联动菜单功能实现

e8279bf59b9b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

html

html只用两个简单的select选择框。

e8279bf59b9b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

js

js代码就是在页面加载完第一次请求后台PHP代码得到第一个选项的子分类,而后每当分类改变时,再次请求后台得到具体的子分类。

e8279bf59b9b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

PHP

前台请求后台的PHP,得到数据进行返回,而PHP的数据大多数是从数据库中得到的,在这里只是一些伪数据

e8279bf59b9b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

源代码

html

content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

Document

.content{

width: 100%;

text-align: center;

margin-top: 50px;

}

.cate{

width: 150px;

height: 30px;

border-radius: 5px;

}

水果

电子商品

服装

$(document).ready(function(){

getDataForAjax();

});

$("#cate").change(function () {

getDataForAjax();

});

function getDataForAjax() {

cate_id = $('#cate').val();

$.ajax({

type:"post",

url:"data.php",

dataType:'json',

data:{

cate_id:cate_id

},

success:function (data) {

console.log(data);

$('#attr').empty();

for(var i = 0;i < data.length;i++){

$('#attr').append(""

+ data[i]['name'] + "");

}

}

});

}

PHP

$category = $_POST['cate_id'];

//得到分类ID,查询数据库,得到分类下有哪些子分类,在这里只是伪数据。

$data = [

[

['id' => 1,'name' => '苹果'],

['id' => 2,'name' => '香蕉'],

['id' => 3,'name' => '橘子'],

['id' => 4,'name' => '梨'],

],

[

['id' => 1,'name' => '手机'],

['id' => 2,'name' => '电脑'],

['id' => 3,'name' => '平板'],

['id' => 4,'name' => '手表'],

],

[

['id' => 1,'name' => '上衣'],

['id' => 2,'name' => '裤子'],

['id' => 3,'name' => '鞋子'],

],

];

//进行json转换,返回数据。

echo json_encode($data[$category]);

代码文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值