【问题标题】:pre-selection of fields with ajax for select2使用 ajax 为 select2 预选字段
【发布时间】:2020-01-24 12:48:17
【问题描述】:

当我尝试使用 ajax 预先选择值时,除了显示的值之外的所有内容都在工作。 有谁知道我在哪里犯了错误?

我为每个<select> 调用pre_select() 函数。 当我查看代码时,一切正常,但页面上的标签显示ID 而不是myTitle。提交表单后,数据也ok了!我只需要正确的标签...

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】:

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

    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
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2017-06-25
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多