select2 4.0.3 预设值

 时间:2024-11-03 22:15:05

1、按照select2 的说明很容易就可以使用select2, 单选,多选都不是问题。但是如果使用预设值呢? 就是 select2 初始化后自动帮我们选定一个或者多个值, 改如何做呢?

2、当数据源不是 ajax时, 可以使用下面的方法设定预设值$('#mySelect2').v锾攒揉敫al('US');$('#mySelect2').trigger('change')

3、当数据源是 ajax 时, 就要使用下面的方法:// Set up the Select2 control改怊眶峋$('#mySelect2').select2({ ajax: { url: '/api/students' }});// Fetch the preselected item, and add to the controlvar studentSelect = $('#mySelect2');$.ajax({ type: 'GET', url: '/api/students/s/' + studentId}).then(function (data) { // 官网上使用ajax 来获得要预设的值, 我是直接使用页面上的隐藏域的值 // create the option and append to Select2 var option = new Option(data.full_name, data.id, true, true); studentSelect.append(option).trigger('change');// manually trigger the `select2:select` eventstudentSelect.trigger({ type: 'select2:select', params: { data: data } });}

select2 4.0.3 预设值
select2 4.0.3 预设值

4、下面的代码是我的实例

select2 4.0.3 预设值
  • select2的简单实用
  • 如何利用select2下拉搜素框设置其默认值
  • select2.js 如何引用
  • select2multiple多选设置默认值
  • 如何利用select2下拉框插件方法限制选框项个数
  • 热门搜索
    学会感恩手抄报 孝心手抄报 年月日手抄报 端午节的手抄报怎么画 九一八手抄报 清明节手抄报一年级 科技知识手抄报 少年中国说手抄报 肺炎手抄报 节约用水的手抄报内容