select2使用经验总结

  • Post author:
  • Post category:其他


select2 版本:


Select2 4.0.6-rc.1

本人新手,初次使用select2组件,遇到很多难题,记录下来帮助其他新手。

<select id=”selectID”></select>

问题1:如何获取select2已选择的数值?

答案:     $(‘#selectID’).select2(‘data’);

问题2:如何给select2赋值?

答案:    $(‘#selectID’).val(new_value).select2();

问题3:如何重新初始化select2选项,比如动态获取数据然后更新select2数据?

答案:    $(‘#selectID’).empty();

$(‘#selectID’).select2({

tags: true,

placeholder: ‘请选择版本’,

data:

data

,

multiple:true,

allowClear: true

})

问题4:如何获取select2选中的选项的属性,比如id?

答案:    $(‘selectID’).find(‘option:checked’).attr(‘id’);



版权声明:本文为ljl6158999原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。