select2用ajax预先选择字段

问题描述 投票:1回答:1

当我尝试使用ajax预先选择值时,除显示的值外,其他所有值都在起作用。有人知道我在哪里犯错了吗?

我为每个pre_select()调用<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>
javascript php ajax jquery-select2 jquery-select2-4
1个回答
0
投票

此功能完成了我的工作:-)

© www.soinside.com 2019 - 2024. All rights reserved.