gpt4 book ai didi

javascript - jquery select2 - 通过 AJAX php 格式化结果

转载 作者:行者123 更新时间:2023-11-30 18:04:27 26 4
gpt4 key购买 nike

我使用 select2,我想像这样格式化我的结果

名字,第一个。

$("#id").select2({
minimumInputLength : 0,
allowClear: true,
ajax : {
url : "Form/page.php",
dataType : 'json',
data : function (term, page) {
return {
q : term
};
},
results: function (data, page) {
return { results : data.ex};
},
formatResult : function formatResult(ex) {
return '<b>' + ex.name + '</b>';
}
}

});

我的 php 文件是这样的

    while($r=mysql_fetch_array($m)) {
$rows['id']=$r['id'];
$rows['text']=$r['name'];
$rows['first']=", ". $r['first'];
$rows2[]=$rows;
}
print json_encode($rows2);

我该怎么做,谢谢

最佳答案

我认为 php 代码必须是这样的:

while($r=mysql_fetch_array($m)) {
$rows['id']=$r['id'];
$rows['name']=$r['name'];
$rows['first']=$r['first'];
$rows2[]=$rows;
}
print json_encode($rows2);

因此,您传递了一个包含 id、name 和 first 的 json 对象数组。

formatResult 的返回更改为:

return '<b>' + ex.name + '</b>, ' + ex.first;

关于javascript - jquery select2 - 通过 AJAX php 格式化结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16161086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com