搜索、自动完成,支持ajax
支持嵌套结构###
<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
},
});
网友评论