1

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

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 );
};
4

1 回答 1

3

我在jsfiddle中有一个带有工作示例的演示。在这里你可以看到它:

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

测试我的演示:

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

于 2012-06-17T17:30:12.380 回答