网站建设资讯

NEWS

网站建设资讯

Jqueryselect三级联动(需要JSON数据)-创新互联

Scripts/Category.js

成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、成都网站制作、成都外贸网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的永康网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
//Jquery三级类别联动$(function () {  
    BindCategory();
})
function BindCategory() {
var $txtCategory = $("#txtCategoryId");
var _selectId = ["selBigClass", "selSmallClass", "selThreeClass"];
for (var i = 0; i < _selectId.length; i++) {
var select = document.createElement("select");
        select.id= _selectId[i];
        select.name= _selectId[i];
        $txtCategory.parent().append(select);
    }
var $selBigClass = $("#selBigClass");
var $selSmallClass = $("#selSmallClass");
var $selThreeClass = $("#selThreeClass");
    $selSmallClass.hide();
    $selThreeClass.hide();
    $.getJSON("Scripts/CategoryJson.js", function (json) {
var html = [''];
for (var key in json) {
if (json[key].pid == 0) {
                html.push('');
            }
        }
//第一级类别        $selBigClass.empty().append(html.join('')).change(function () {
            $txtCategory.val(this.value);
            $selThreeClass.hide();
            $selSmallClass.show();
var html = [''];
for (var data in json) {
if (json[data].pid == $("#selBigClass").val()) {
                    html.push('');
                }
            }
//第二级类别            $selSmallClass.empty().append(html.join('')).change(function () {
                $txtCategory.val(this.value);               
                $selThreeClass.show();
var html = [''];
for (var data in json) {
if (json[data].pid == $selSmallClass.val()) {
                        html.push('');
                    }
                }
//第三级类别                $selThreeClass.empty().append(html.join('')).change(function () {
                    $txtCategory.val(this.value);
                });
//如果不存在第三级类别则隐藏第三级类别控件  if ($selThreeClass.get(0).options.length <= 1) {
                    $selThreeClass.hide();
return false;
                }
            });
//如果不存在第二级类别则隐藏第二级类别控件 if ($selSmallClass.get(0).options.length <= 1) {
                $selSmallClass.hide();
return false;
            }
           
        });
    });
};

Scripts/CategoryJson.js(如何生成json数据详见:http://www.cnblogs.com/haozhenjie819/p/3601676.html)

[{ "id": 31, "name": "u65b0u95fbu4e2du5fc3", "pid": 0, "itemvalue": "|0|" },
{"id": 59, "name": "u52a0u76dfu5408u4f5c", "pid": 0, "itemvalue": "|0|" },
{"id": 65, "name": "u4ea7u54c1u670du52a1", "pid": 0, "itemvalue": "|0|" },



{"id": 60, "name": "u52a0u76dfu987bu77e5", "pid": 59, "itemvalue": "|0|59|" },
{"id": 61, "name": "u52a0u76dfu6d41u7a0b", "pid": 59, "itemvalue": "|0|59|" },
{"id": 62, "name": "u52a0u76dfu533au57df", "pid": 59, "itemvalue": "|0|59|" },



{"id": 70, "name": "u63a8u8350u670du52a1", "pid": 65, "itemvalue": "|0|65|" },
{"id": 71, "name": "u8ba1u8d39u65b9u5f0f", "pid": 65, "itemvalue": "|0|65|" },
{"id": 72, "name": "u7528u6237u987bu77e5", "pid": 65, "itemvalue": "|0|65|" },
{"id": 73, "name": "u7981u5bc4u8303u56f4", "pid": 65, "itemvalue": "|0|65|" },
{"id": 75, "name": "u666eu901au5febu9012", "pid": 70, "itemvalue": "|0|65|70|" },
{"id": 76, "name": "u7279u79cdu5febu4ef6", "pid": 70, "itemvalue": "|0|65|70|" },
{"id": 77, "name": "u9650u65f6u6d3eu9001", "pid": 70, "itemvalue": "|0|65|70|" },
{"id": 78, "name": "u4ee3u6536u8d37u6b3e", "pid": 70, "itemvalue": "|0|65|70|" },
{"id": 79, "name": "u4fddu4ef7u670du52a1", "pid": 70, "itemvalue": "|0|65|70|" }, 
{"id": 80, "name": "u4ee3u7b7eu56deu5355", "pid": 70, "itemvalue": "|0|65|70|"}]
View Code

html页面

  三级联动         
 
新闻类别:

效果图

知识点:1、Jquery获取select中option个数

$("#selSmallClass").get(0).options.length


网站栏目:Jqueryselect三级联动(需要JSON数据)-创新互联
转载来源:http://cdweb.net/article/ddpsjg.html