Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax为select2预选字段_Javascript_Php_Ajax_Jquery Select2_Jquery Select2 4 - Fatal编程技术网

Javascript 使用ajax为select2预选字段

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

当我尝试使用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);
    $.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
        });
    });
}