jquery级联下拉框插件

;(function($) {        
    $.fn.cascade = function(options) {
        var defaults = {
            createOptions: function(parentId){
                var rets = [];
                rets.push('<option value="">-- 请选择 --</option>');
                return rets;
            }
        };
        var opts = $.extend(defaults, options);
        var $root = $(this); 
        $root.each(function(k, v){
            if($(this).next()!=undefined){
                var val = $(this).next().val();
                var parentId = null;
                if(k>0){
                    parentId = $($root[k-1]).next().val();
                }
                if(k==0 || (k>0 && parentId!=null)){
                    var rets = opts.createOptions(parentId);
                    var html = "";
                    if($.type(rets)=="array"){
                        html = rets.join("");
                    }else if($.type(rets)=="string"){
                        html = rets;
                    }
                    $(this).html(html);
                    $(this).val(val);
                }
            }
        });
        $root.change(function(){
            var currVal = $(this).val();
            var index = $root.index(this);
            if(index+1<$root.length){
                var $next = $($root[index+1]);
                $next.empty();//清空下级菜单
                if(index==0 || (index>0 && currVal!="")){
                    var rets = opts.createOptions(currVal);
                    var html = "";
                    if($.type(rets)=="array"){
                        html = rets.join("");
                    }else if($.type(rets)=="string"){
                        html = rets;
                    }
                    $next.html(html);
                    $next.val("").trigger("change");
                }
            }
        });
    };
})(jQuery); 

 

html

<div class="form-group">
                                    <label for="" class="col-sm-2 control-label">研究领域:</label>
                                    <div class="col-sm-9">
                                        <div class="row">
                                            <input type="hidden" id="professionId" value="${project.profession.id}"/>
                                            <div class="col-sm-4">
                                                <select class="form-control profession"></select>
                                                <c:if test="${!empty project.profession.id}">
                                                    <input type="hidden" value="${fn:substring(project.profession.id, 0, 3)}" />
                                                </c:if>
                                            </div>
                                            <div class="col-sm-4">
                                                <select class="form-control profession"></select>
                                                <c:if test="${!empty project.profession.id && project.profession.level>1}">
                                                    <input type="hidden" value="${fn:substring(project.profession.id, 0, 6)}" />
                                                </c:if>
                                            </div>
                                            <div class="col-sm-4">
                                                <select class="form-control profession"></select>
                                                <c:if test="${!empty project.profession.id && project.profession.level>2}">
                                                    <input type="hidden" value="${fn:substring(project.profession.id, 0, 8)}" />
                                                </c:if>
                                            </div>
                                        </div>
                                    </div>
                                </div>

说明:select后跟一个隐藏的input存放用来初始化的值

 

js调用

$("select.profession").cascade({
        createOptions: function(parentId){
            var rets = [];
            rets.push('<option value="">-- 请选择 --</option>');
            $.ajax({
                type: "POST",
                url: "/profession/getProfessions",
                data:{
                    parentId:parentId
                },
                async:false,
                success: function(data){
                    if(data){
                        $.each(data,function(i){
                            rets.push('<option value="'+data[i].id+'">'+data[i].name+'</option>');
                        });
                    }
                }
            });
            return rets;
        }
    });

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值