Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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选择2个预定义的渲染项_Javascript_Jquery_Ajax_Jquery Select2 - Fatal编程技术网

Javascript 使用ajax选择2个预定义的渲染项

Javascript 使用ajax选择2个预定义的渲染项,javascript,jquery,ajax,jquery-select2,Javascript,Jquery,Ajax,Jquery Select2,我可以轻松地检索和格式化AJAX数据以使用Select2,如下所示 function formatItem (item) { if (!item.id) { return item.text; } var _item = '<span class="' + item.deleted +'"><i class="fa fa-user"></i> ' + item.doctor + ', <small>' + i

我可以轻松地检索和格式化AJAX数据以使用Select2,如下所示

function formatItem (item) {
    if (!item.id) {
        return item.text;
    }
    var _item = '<span class="' + item.deleted +'"><i class="fa fa-user"></i> ' + item.doctor + ', <small>' + item.sex + ' (' + item.user + ')</small></span>';
    return $(_item);
}

var tabSelect = $('#t');
tabSelect.select2({
    ajax: {
        url: '../ajax/results.php',
        data: function (params) {
            return {
                search: params.term,
                page: params.page || 1
            };
        }
    },
    templateResult: formatItem,
    templateSelection: formatItem
});
我得到了HTML呈现项,但带有一系列未定义的
(请注意
console.log(item)
返回预期结果:换句话说,我有字段,但不知道如何呈现它们)

我尝试了很多东西,但都不管用,例如:

var option = new Option(formatItem(item), item.id, true, true);

我四处阅读,但找不到有效的解决方案:请,有什么帮助吗?

我通过在JSON响应中返回整个HTML片段(作为
text
item)成功地解决了这个问题

//JSON响应
{
“结果”:[
{
“id”:“1234”,
“文本”:“Doe John先生(场地所有者)”
}
]
}
//更新的代码段
$.ajax({
键入:“GET”,
url:“../ajax/results selected.php”,//检索单个项目的url
数据:{
iData:[这里是检索单个项目的ID]
}
}).then(功能(数据){
//创建选项并附加到Select2
var项目=数据。结果[0];
控制台日志(项目);
var option=新选项(item.text、item.id、true、true);
tabSelect.append(option.trigger('change');
//手动触发'select2:select'事件
tabSelect.trigger({
键入:“select2:select”,
参数:{
数据:数据
}
});
});
var option = new Option(formatItem(item), item.id, true, true);
// the JSON response
{
    "results": [
        {
            "id": "1234",
            "text": "<span class=\"\"><i class=\"fa fa-user\"></i> Doe John, <small>Mr. (Site Owner)</small></span>"
         }
    ]
}


// the updated snippet
$.ajax({
    type: 'GET',
    url: '../ajax/results-selected.php', // url to retrieve a single item
    data: {
        iData: [here goes the ID for retrieving single item]
    }
}).then(function (data) {
    // create the option and append to Select2
    var item = data.results[0];
    console.log(item);
    var option = new Option(item.text, item.id, true, true);
    tabSelect.append(option).trigger('change');

    // manually trigger the `select2:select` event
    tabSelect.trigger({
        type: 'select2:select',
        params: {
            data: data
        }
    });
});