Javascript 使用ajax为select2预选字段
当我尝试使用ajax预先选择值时,除了显示的值之外,其他一切都正常工作。 有人知道我在哪里犯的错误吗 我为每个Javascript 使用ajax为select2预选字段,javascript,php,ajax,jquery-select2,jquery-select2-4,Javascript,Php,Ajax,Jquery Select2,Jquery Select2 4,当我尝试使用ajax预先选择值时,除了显示的值之外,其他一切都正常工作。 有人知道我在哪里犯的错误吗 我为每个调用pre\u select()函数。 当我查看代码时,一切正常,但页面上的标签显示的是ID,而不是myTitle。提交表单后,数据也可以!我只需要正确的标签 function pre_select(pre_id, query_id) { //my ID of selection, the query var mySelect = $('#form_my_id'+pre_id);
调用pre\u select()
函数。
当我查看代码时,一切正常,但页面上的标签显示的是ID
,而不是myTitle
。提交表单后,数据也可以!我只需要正确的标签
function pre_select(pre_id, query_id) { //my ID of selection, the query
var mySelect = $('#form_my_id'+pre_id);
$.ajax({
type: 'GET',
dataType:'json',
url: '?search&id='+query_id
}).then(function (data) {
var option = new Option(data.myTitle, data.id, true, true);
mySelect.append(option).trigger('change');
mySelect.trigger({
type: 'select2:select',
params: {
data: data
}
});
});
}
这是HTML输出
<select name="form_select[]" class="form-control select2bs4 select2-hidden-accessible" id="form_my_id1" width="100%" required="" data-select2-id="form_stok_id1" tabindex="-1" aria-hidden="true">
<option value="1" selected="" data-select2-id="5">The Title Of Product</option>
</select>
产品名称
此函数已完成我的工作:-)
此函数已完成我的工作:-)
function pre_select(pre_id, query_id) { //my ID of selection, the query
var mySelect = $('#form_my_id'+pre_id);
$.ajax({
type: 'GET',
dataType:'json',
url: '?search&id='+query_id
}).then(function (data) {
mySelect.select2("trigger", "select", {
data: data
});
});
}