美文网首页
jquery插件chosen 选择框

jquery插件chosen 选择框

作者: 小呆糊总 | 来源:发表于2019-03-20 15:07 被阅读0次

为解决下拉框内容过长问题,如下图:

级联选择:先选择企业,再选择评价对象,如果有值,下面的评价对象依旧是带有搜索框的下拉框,如果对应企业下面没有评价对象,显示‘无符合该项标准评价对象’
image.png
image.png
image.png
1.html引入
   <div class="row cl">
         <label class="form-label col-xs-3 col-sm-2"> 评价对象
                   <span class="c-red">*</span>
         </label>
         <div class="formControls col-xs-8 col-sm-8">
                    <span class="select-box radius col-sm-5" style="width:40%;height: 31px;">
                        <!-- 企业列表下拉框 -->
                        <select class=" chosen-select companyList"  name="companyList" style="font-size: 14px;border: none;"></select>
                    </span>
                    <span class="select-box radius targetIdContent col-sm-5 col-sm-offset-1 hide" style="width:40%;height: 31px;">
                        <!-- 评价对象下拉框 -->
                         <select class="select chosen-select targetLi"  name="gradeWays"></select>
                    </span>
                    <input type="hidden" class="companyId">
          </div>
    </div>

    <link href="../../assets/chosen/chosen.css" rel="stylesheet">
    <script src="../../assets/chosen/chosen.jquery.min.js"></script>
    <!-- 模板获取企业下拉框列表 -->
    <script src="../../assets/template.js"></script>

//html底部js引入
    <!-- 评价对象选择时-先选择企业 企业列表的获取 用template实现 -->
    <script type="text/template7" id="companyListTemplate">
        <option value="">请选择企业</option>
        {{each data value i}}
        <option value="{{value.id}}">{{value.companyName}}</option>
        {{/each}}
    </script>
    <script type="text/javascript">
        $(function () {
            companyList();//获取企业下拉列表
            //下拉框带搜索
            $('.chosen-select').chosen({
                allow_single_deselect: true,
                search_contains: true,//搜索不是从开头搜索,而是包含搜索
            });
            //resize the chosen on window resize
            $(window).off('resize.chosen').on('resize.chosen', function () {
                $('.chosen-select').each(function () {
                    var $this = $(this);
                    $this.next().css({
                        'width': $this.parent().width()
                    });
                })
            }).trigger('resize.chosen');
        })
    </script>
2.js
   // 企业的获取列表
    function companyList(){
        $.ajax({
            url: basePath + "/companyList",
            type: "POST",
            dataType: "json",
            success: function (response) {
                if (response.status) {
                    var html = template('companyListTemplate', response)
                    $('.companyList').html(html)
                    chose_set_ini($(".companyList"), 0);//企业下拉框带搜索
                    $(".companyList").on('change', function(event){
                      //从下拉框选择企业时,获取评价对象列表
                        var companyId=$(this).val();
                        $('.companyId').val(companyId)
                         getTargetList(companyId)
                    });
                }
            }
        });
    }
    //评价对象获取列表
    function getTargetList(companyId){
        $.ajax({
            url: basePath + '/targetList',
            type: 'post',
            data:{
                companyId:companyId,
            },
            dataType: "json",
            success: function (response) {
                if (response.status) {
                    if(response.data.length>0){//如果评价对象过去列表有值
                         //销毁select后列表有值,再给.targetIdContent添加select
                        $('.targetIdContent').html('<select class="select chosen-select targetLi"  name="gradeWays"></select>')
                        var html = '<option value="">请选择评价对象</option>'
                        for(var i=0;i<response.data.length;i++){
                            html +='<option  value="'+response.data[i].id+'" index="'+i+'" >'+response.data[i].name+'</option>'
                        }
                        $('.targetLi').html(html)
                        chose_set_ini($(".targetLi"), 0);//评价对象下拉框带搜索
                    }else{//如果评价对象过去列表没数据,提示信息“无符合该项标准评价对象”
                        /*销毁select 清除评价对象下拉列表的option,和已选择的option,
                        如果不销毁会导致在企业切换时,评价对象由有数据切换至无列表数据时,虽然提示了“无符合该项标准评价对象”,
                        点击下拉仍然会出现之前的数据*/
                        $('.targetLi').chosen("destroy")
                        $('.targetIdContent').html('<p style="color:red">无符合该项标准评价对象</p>')
                    }
                } else {
                    layer.msg(response.message, {icon: 5,time:2000});
                }
            }
        });
    }
    //下拉框带搜索
    function chose_set_ini(select, value) {
        $(select).val(value);
        $(select).trigger("chosen:updated");
    }

相关文章

  • jquery插件chosen 选择框

    为解决下拉框内容过长问题,如下图: 级联选择:先选择企业,再选择评价对象,如果有值,下面的评价对象依旧是带有搜索框...

  • jquery chosen插件使用及select常用方法

    1、chosen插件使用 chosen插件依赖于jQuery库或prototype,使用之前要先引入jQuery或...

  • js插件网址

    1.jQuery内容滚动插件-BoxSlider2.jQuery artDialog对话框插件3.移动端日期选择组...

  • 常用插件

    1.jQuery内容滚动插件-BoxSlider 2.jQuery artDialog对话框插件 3.移动端日期选...

  • js内容汇总

    一、ajax请求(jquery) 二、Jquery 弹出对话框插件xcConfirm.js https://blo...

  • jQuery插件 hank编写 (输入框默认提示 detext,

    jQuery插件 hank编写 (输入框默认提示 detext, form检验插件 form_check, 自定义...

  • js实现类bootstrap模态框动画

    在pc端开发,模态框是一个很常用的插件,之前一直用的第三方插件,比如bootstrap,jQuery的模态框插件,...

  • jQuery插件

    1.jQuery插件分类 封装对象方法的插件 应用最广 封装全局函数的插件 作为jQuery全局函数插件 选择器插...

  • 40款经典前端特效插件---分享

    1.flavr—超级漂亮的jQuery扁平弹出对话框 2.轻量级触摸响应滑块插件JQuery lightSlid...

  • 理解Bootstrap模态框

    在本教程中,我们将讨论十分有用的 Bootstrap jQuery插件——模态框。 Bootstrap 模态框是一...

网友评论

      本文标题:jquery插件chosen 选择框

      本文链接:https://www.haomeiwen.com/subject/jugamqtx.html