select2摘要

搜索、自动完成,支持ajax

官网:https://select2.github.io

支持嵌套结构###

<select>
  <optgroup label="Group Name">
    <option>Nested option</option>
  </optgroup>
</select>

初始化选择值,用于编辑###

      var changids = [];
      changIds = res.ids;
      savantSelect2.val(changIds).trigger('change'); //set the value  

事件监听###

$('select').on('select2:select', function (evt) {
  // Do something
});

获取选取##

    $('#select-user').on('select2:select', function (evt) {
        // Do something
        console.log($(this).find('option:selected').text());
    });

ajax remote 数据###

$('#barrier-sub-type').select2({
        ajax: {
            url: url,
            dataType: 'json',
            delay: 150,
            data: function (params) {
                return {
                    q: params.term // search term
                };
            },
            processResults: function (res) {
                // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data
                return {
                    results: $.map(res.data, function (item) {
                        return {
                            text: item.name,
                            id: item.id
                        }
                    })
                };
            },
            cache: true
        },
    });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,985评论 25 708
  • 暑期第二天,早上七点起床,看一会儿书,上午写材料。总结部分不难,教学与德育方面有些难。 教学方面需要...
    栖居侠客阅读 258评论 0 0
  • 在大数据时代,数据可视化是多么的重要,特别是对领导者、数据分析师、运营等职位的朋友,经常和数据打交道,能够做到数据...
    UIleader阅读 439评论 0 2
  • 今天下午花3个小时的时间读完了黑塞的《悉达多》。这是一本有关佛陀关于修行的书籍,读起来很有禅意,适合各个层面的人阅...
    夏虫bu语阅读 716评论 2 9