gpt4 book ai didi

php - 未捕获的类型错误。 Item.replace 不是函数,autoComplete 插件

转载 作者:行者123 更新时间:2023-12-01 07:43:50 24 4
gpt4 key购买 nike

我正在使用 jQuery 插件 autoComplete https://goodies.pixabay.com/jquery/auto-complete/demo.html ,我正在尝试使用他们的 ajax 请求,不幸的是我遇到了错误。

上面写着

item.replace is not a function at object.renderItem

$.fn.autoComplete.defaults = {
source: 0,
minChars: 3,
delay: 150,
cache: 1,
menuClass: '',
renderItem: function (item, search){
// escape special characters
search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
return '<div class="autocomplete-suggestion" data-val="' + item + '">' + item.replace(re, "<b>$1</b>") + '</div>';
},
onSelect: function(e, term, item){}
};

这是我的代码

$('.to_msg').autoComplete({
source: function(term, response){
$.getJSON('ajax/auto_complete_ajax.php', { q: term },
function(data){
response(data);
});
}
});

还有我的 PHP 响应

$recipient = $_GET['q'];
$a_json = array();
$a_json_row = array();


//restrict the store
$query_filter = $db->query("SELECT * from master_data.user_account where Name like '%$recipient%'");
while($result_filter = $query_filter->whileFetch()){
$name = htmlentities(stripcslashes($result_filter['Name']));
$user_id = htmlentities(stripcslashes($result_filter['user_id']));
$a_json_row['user_id'] = $user_id;
$a_json_row['value'] = $name;
$a_json_row['label'] = $name;
array_push($a_json, $a_json_row);
}

echo json_encode($a_json);
flush();

这是 JSON 输出

[
{"user_id":"5","value":"Melde Feliciano ","label":"Melde Feliciano "},
{"user_id":"46","value":"Melanie Charlton ","label":"Melanie Charlton "}
]

最佳答案

renderItem(item, search) 中的

item 是一个对象。尝试将 item.replace() 替换为 item.label.replace()

$.fn.autoComplete.defaults = {
source: 0,
minChars: 3,
delay: 150,
cache: 1,
menuClass: '',
renderItem: function (item, search){
// escape special characters
search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
return '<div class="autocomplete-suggestion" data-val="' + item.value + '">' + item.label.replace(re, "<b>$1</b>") + '</div>';
},
onSelect: function(e, term, item){}
};

关于php - 未捕获的类型错误。 Item.replace 不是函数,autoComplete 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42195498/

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