【发布时间】:2019-12-23 10:47:08
【问题描述】:
这是我的 select2 字段的 html 代码。
<select data-init-plugin="select2" data-max="4" multiple="" id="mls" name="mls" class="select2 form-control full-width ajax-supported select2-hidden-accessible" data-callback="getAgents" tabindex="-1" aria-hidden="true"></select>
这是 select2 JavaScript 代码。
$('.ajax-supported').select2({
ajax: {
dataType: 'json',
multiple: true,
type: "POST",
data: function (term) {
return {
'_token': $('#_token').val(),
name: $(this).attr('name'),
callback: $(this).data('callback'),
q: term.term,
};
},
url: '{{ url('listing-field-ajax-callback') }}',
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.itemName,
id: item.id
}
})
};
},
},
minimumInputLength: 1,
maximumSelectionLength: maximum_selectable_items, // this is where i want 4 from data attribute data-max=4
});
现在我想获取数据属性 data-max = 4 并且我想在我的 maximumSelectionLength 中使用它并想控制所选项目的数量。如何从选择的 HTML 中获取数据属性并在我的 JavaScript 中使用它?
【问题讨论】:
标签: javascript jquery select jquery-select2