3

我想用下面的代码模仿谷歌的建议,意思是:

第 1 步:当用户在搜索框中键入时,查询字符串将由服务器 php 文件处理并返回查询建议字符串(使用 Ajax 对象)。

第 2 步:当用户点击查询建议时,它会填入搜索框(自动完成)。

第 1 步已完成,第 2 步未完成。我认为问题在于 .click() 方法(我稍后使用 .live() ,但它仍然无法正常工作)。我的意图是使用 .live() 或 .click() 将 onclick 事件绑定到动态创建的<li>元素。任何想法?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script src="jquery-1.4.2.js">
</script>

<style>
#search,#suggest,ul,li{margin: 0; padding:0; width: 200px;}
ul{ list-style-type: none;}
.border{border: solid red 1px; }
</style>

<p>My first language is:</p>
<input type="text" width="200px" id="search" onkeyup="main(this.value)" value="" />
<ul id="suggest"></ul>

<script type="text/javascript">
$(function main(str)
{  //binding any forthcoming li element click event to a function
$('li').live('click', function(){ $("#search").val(array[i]);});
  //setup Ajax object 
  var request=new XMLHttpRequest();
  request.open("GET","language.php?q="+str,true)
  //core function
  request.onreadystatechange=function()
    {    
  if ( request.readyState==4 && request.status==200)
       {  if (str=="") {$('li').remove(); $('ul').removeClass('border');return;}
      $('li').remove();
      reply=request.responseText.split(",");
         for (i=0;i<reply.length;i++)
         {
          //create HTML element of <li>
         $('#suggest').append($('<li>',{id: 'li'+i, html: reply[i]}));
         //style ul
         $('ul').addClass('border');
              }       
      }
    }
  request.send(); 
})
</script>

PHP:

<?php
$q=$_GET[q];

$a[]='english';
$a[]='chinese';
$a[]='japanese';
$a[]='eeeeee';

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
  $hint="";
  for($i=0; $i<count($a); $i++)
  {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
    {
      if ($hint=="")
      {
        $hint=$a[$i];
      }
      else
      {
        $hint=$hint." , ".$a[$i];
      }
    }
  }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
  $response="no suggestion";
}
else
{
  $response=$hint;
}

//output the response
echo $response;
?>
4

5 回答 5

3

您正在寻找 jQuery Autocomplete

于 2010-06-06T14:40:44.863 回答
2

你是正确的关于哪条线是问题。您缺少#按 ID 搜索的功能。

$('li'+i).click(function(){ $("#search").html(array[i]);});

应该

$('#li'+i).click(function(){ $("#search").html(array[i]);});

然而,有很多更简洁的方法可以做到这一点,不需要重新查询文档来附加这个处理程序。我完全同意使用插件的建议。

于 2010-06-06T15:05:36.403 回答
2

此外,您可能希望等待用户空闲。这可以防止过多的往返。这将意味着编写如下内容:

$("input").keyUp(function(e) {
  clearTimeout(updater);
  updater = setTimeout(whenReady, 200);
}

function whenReady() {
  // update the search box here...
}
于 2010-06-06T15:13:27.247 回答
1

我浏览了您的代码,其中几乎没有问题。1)如果你想在动态创建的元素上绑定点击事件,那么你应该使用 .live('click', function(){}) 事件绑定器。这个 jQuery 函数将在选择器上绑定 click 事件,该事件稍后将在代码中动态创建,因此如果您在 document ready 函数上编写 live() 事件,来自服务器的元素将自动绑定到 click 事件。阅读文档

这是示例代码

<script>
$(function() {
  $("#suggest li").live('click', function() {
    $("#search").val($(this).text()); // li inner html contains text that needs to put into search box
    alert($(this).text()); // or  alert(array[i]); in your code 
    //c what is the out put of above code. better if you change name of an array
  });
});
</script>

文本输入元素值也是在代码中使用 .val() 函数而不是 .html 函数获取的$("#search").html(array[i]);

问候阿亚兹阿拉维

于 2010-06-06T15:10:30.687 回答
-1

如果您希望您的应用程序正常工作,您还应该考虑缓存响应,例如在退格的情况下。

于 2010-06-06T14:42:28.440 回答