Php Jquery-自动完成,带有可点击链接
我已经用jqueryUI autocomplete实现了搜索模块,在该模块中,搜索建议以其类别呈现。Php Jquery-自动完成,带有可点击链接,php,jquery,autocomplete,Php,Jquery,Autocomplete,我已经用jqueryUI autocomplete实现了搜索模块,在该模块中,搜索建议以其类别呈现。 现在我想让这些建议可点击,我已经尝试了选择功能,但它不起作用,现在我坚持使用它 我的Jquery代码是 $( function() { $.widget("custom.catcomplete", $.ui.autocomplete, { _renderMenu: function(ul, items) { var that = this, currentCa
现在我想让这些建议可点击,我已经尝试了选择功能,但它不起作用,现在我坚持使用它
我的Jquery代码是
$( function() {
$.widget("custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function(ul, items) {
var that = this,
currentCategory = "";
$.each(items, function(index, item) {
if (item.category != currentCategory) {
ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
currentCategory = item.category;
}
that._renderItemData(ul, item);
});
}
});
$( "#search" ).catcomplete({
source : function(request,response){
var term = $("#search").val();
$.ajax({
type : "GET",
url : "ajax/auto_search.php",
dataType : "json",
data : {term : term},
success : function(data)
{
console.log(data);
response($.map(data.list, function(item) {
return {
value: item.content_title,
label: item.content_title,
category: item.category_name,
url : item.url
}
}))
},
select: function( event, ui ) {
//main problem is with item.content_title unable to get its value
window.location.href = 'search.php?mode=content_type&query='+item.content_title;
}
});
}
});
});
$(函数(){
$.widget(“custom.catcomplete”,$.ui.autocomplete{
_renderMenu:功能(ul,项目){
var=这个,
currentCategory=“”;
$。每个(项目、功能(索引、项目){
如果(item.category!=当前类别){
ul.追加(“”+item.category+“ ”);
currentCategory=item.category;
}
即._renderItemData(ul,项目);
});
}
});
$(“#搜索”).catcomplete({
来源:功能(请求、响应){
var term=$(“#搜索”).val();
$.ajax({
键入:“获取”,
url:“ajax/auto_search.php”,
数据类型:“json”,
数据:{term:term},
成功:功能(数据)
{
控制台日志(数据);
响应($.map(data.list,函数(项)){
返回{
值:item.content\u title,
标签:item.content\u title,
类别:item.category\u名称,
url:item.url
}
}))
},
选择:函数(事件,ui){
//主要问题是item.content\u title无法获取其值
window.location.href='search.php?mode=content\u type&query='+item.content\u title;
}
});
}
});
});
php代码是
<?php
include '../includes/dbconfig.php';
header('Content-Type: application/json');
$searchTerm = $_GET['term'];
$sql = "SELECT distinct content_title, c.content_title,p.category_name,c.content_url from content_ref_table c inner join category_ref_table p on p.category_id = c.category_id where c.content_title LIKE '%".$searchTerm."%' ORDER BY c.content_title ASC LIMIT 0,10";
$result = mysqli_query($conn,$sql) or die(mysqli_error($conn));
$data_array = array();
while($row = mysqli_fetch_array($result))
{
$data_array['list'][] = array("content_title" => $row['content_title'], "category_name" => $row['category_name'],"url" => $row['content_url'] );
}
echo json_encode($data_array);
?>
**编辑:如何将item.content\u title存储在全局变量中?我认为如果我能够将item.content\u title存储在变量中,那么它可以解决我的问题**