JQuery KeyUp Live Search。 如何?

我试图找出为什么我可以让我的实时搜索工作但它返回所有结果来自mysql表无论我输入什么。 也许你可以帮忙吗?

我正在尝试获取上一个请求并在每个密钥上启动一个新请求。

   Help Tool 2.0      $(function(){ $('#search-box').keyup(function() { $("#results").html(''); var xhr; var keywords = $(this).val(); if(xhr != null) xhr.abort(); xhr = $.get("search.php", {q: keywords}, function() { //alert("success"); }) .success(function(data) { xhr = null; //alert("second success"); $("#results").html(data); }) }); });   

和PHP:

 <?php include_once ('database_connection.php'); if(isset($_GET['q'])){ $keyword = trim($_GET['q']) ; $keyword = mysqli_real_escape_string($dbc, $keyword); $query = "select topictitle,topicdescription from topics where topictitle like '%$q%' or topicdescription like '%$q%'"; //echo $query; $result = mysqli_query($dbc,$query); if($result){ if(mysqli_affected_rows($dbc)!=0){ while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ echo '

'.$row['topictitle'].' '.$row['topicdescription'].'

'; } }else { echo 'No Results for :"'.$_GET['q'].'"'; } } }else { echo 'Parameter Missing'; } ?>

试试这个js代码代替你拥有的东西。 我添加了延迟function,以便脚本在用户停止键入之后等待指定的时间,然后再发送请求。 这可以防止大量请求被发送到服务器。