我的网站上有几个select2框,我想单独访问并单独填写。在迁移到select2 4.0之前,我总是对每个输入(隐藏)标记使用a,而select2 4.0+不再支持这些标记。目前我的代码看起来像这样:
$(document).ready(function() {
function format(item) { return item.text; };
var guestarray = <?php echo json_encode($guestarray);?>;
var datumarray = <?php echo json_encode($datumarray);?>;
$.fn.select2.amd.require([
'select2/data/array',
'select2/utils'
], function (ArrayData, Utils) {
function CustomData ($element, options) {
CustomData.__super__.constructor.call(this, $element, options);
}
Utils.Extend(CustomData, ArrayData);
CustomData.prototype.query = function (params, callback) {
var data = {
results: guestarray
};
callback(data);
};
$(".SELECT").select2({
data:{ results: guestarray, text: 'text' },
formatSelection: format,
multiple: true,
maximumSelectionLength: 3,
formatResult: format,
dataAdapter: CustomData
});
});
});
这样可行,但我不能单独选择select2框,这意味着有些maximumSelectionLength
,有些根本没有选项,具体取决于此时未使用的var datumarray
。 HTML看起来像这样:
<form action="index.php?site=keintrag" method="post">
<div style="display: inline">
<select class="SELECT"
id="SEL-<?php echo(date("Hi", $m_time1 )); ?>" style="width: 50%"></select>
<button name="eintragenvonkunden">Eintragen</button>
</div>
看到我已经为每个select2框使用了一个ID,该ID基于我稍后想要在php中访问的时间来继续使用发布的数据。如何在Javascript中使用该ID根据datumarray
单独填充select2,设置不同的maximumSelectionLenghts并设置不同的initSelections?
Datumarray看起来像这样:
$datumarray = array (
"id" => $rowD['id'],
"datum" => $rowD['datum'], //date
"zeit" => $rowD['zeit'], //time (id is based on it)
"maxinputs" => $rowD['maxinputs'], //maximumSelectionLenghts
"k_nrs" => $rowD['k_nrs'], //initselection
);