分享

根据参数显示类别(三级联动,需要JSON数据)

 昵称10504424 2014-08-06

Scripts/Category.js

调用方法:

绑定三级联动(详情见:http://www.cnblogs.com/haozhenjie819/p/3887279.html)

复制代码
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 = ['<option value="">请选择大类</option>'];
        for (var key in json) {
            if (json[key].pid == 0) {
                html.push('<option value="' + json[key].id + '">' + json[key].name + '</option>');
            }
        }
        //第一级类别
        $selBigClass.empty().append(html.join('')).change(function () {
            $txtCategory.val(this.value);
            $selThreeClass.hide();
            $selSmallClass.show();
            var html = ['<option value="">请选择小类</option>'];
            for (var data in json) {
                if (json[data].pid == $("#selBigClass").val()) {
                    html.push('<option value="' + json[data].id + '">' + json[data].name + '</option>');
                }
            }
            //第二级类别
            $selSmallClass.empty().append(html.join('')).change(function () {
                $txtCategory.val(this.value);               
                $selThreeClass.show();
                var html = ['<option value="">请选择小类</option>'];
                for (var data in json) {
                    if (json[data].pid == $selSmallClass.val()) {
                        html.push('<option value="' + json[data].id + '">' + json[data].name + '</option>');
                    }
                }
                //第三级类别
                $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)

web页面(给hidden赋值,类别编号为75)

效果图

 

 

 

 

 

 

 

涉及的知识点:【JavaScript split() 方法】返回值是一个字符串数组。

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约