gpt4 book ai didi

php - Jquery UI 自动完成项目链接

转载 作者:行者123 更新时间:2023-11-30 13:18:46 25 4
gpt4 key购买 nike

我想做一个搜索框,你可以在其中搜索数据库中的记录。

search.php 看起来像这样:

<?php
// connect to mysql
require_once('includes/connect.php');
// include config
include('includes/config.php');

$nameser = $_GET['term'];

$names = array();
$result = mysql_query("SELECT name,customerid FROM customers WHERE name LIKE '%".$nameser."%' ORDER BY name ASC");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['id'] = $row['customerid'];
$row_array['value'] = htmlentities($row['name']);

array_push($names,$row_array);
}

echo json_encode($names);

?>

Javascript 部分如下所示:

    $("#tags").autocomplete({
source: "search.php",
minLength: 2,
select: function(event, ui) {
window.location = ("customers.php?action=view&cid=" + item.id)
//$('#tags').val(ui.item.id);
//return false;
}
})
.data("autocomplete")._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a href='customers.php?action=view&cid="+ item.id + "'>"+ item.label + "</a>" )
.appendTo( ul );
};

但是,这给出了错误:

ReferenceError: item is not defined

我想要发生的是,当我点击结果列表中的某些内容时,我会被重定向到 url

customers.php?action=view&cid={CustomerID}

有人知道吗?

编辑:

正确的 Javascript 代码:

$("#tags").autocomplete({
source: "search.php",
minLength: 2,
select: function(event, ui) {
window.location = ("customers.php?action=view&cid=" + ui.item.id)
//$('#tags').val(ui.item.id);
//return false;
}
})
.data("autocomplete")._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a href='customers.php?action=view&cid="+ item.id + "'>"+ item.label + "</a>" )
.appendTo( ul );
};

最佳答案

我在 jsfiddle. you can see it here: 中有一个带有工作示例的演示

关键是在autoselect select 方法中获取一个有效的url 值。请务必 console.log ui 值,以便您可以看到可供您使用的内容。我能够使用标准的 ui.item.value 值将用户发送到另一个页面。

测试我的演示:

1.) 进入输入框并输入“a”。2.) 选择“http://www.utexas.edu”3.) 新页面将加载。

关于php - Jquery UI 自动完成项目链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073221/

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