You are here

请教有关 jquery 问题 --- 自动搜索提示功能 。

frank 的头像
Submitted by frank on 星期三, 2009-07-29 02:10

自动搜索提示功能

$form['keys'] = array( //这是搜索框
    '#type' => 'textfield',
    '#title' => $prompt,
    '#default_value' => $keys,
    '#size' => $prompt ? 40 : 20, //from search_form(). hacky little way of dealing with block vs page.
    '#maxlength' => 255,
   '#attributes' => array ('onkeyup' => 'lookup(this.value);','onblur' => 'fill();'),  //两个监听事件
  );

下面是输出HTML:<input type="text" maxlength="255" name="keys" id="edit-keys" size="40" value="test" onkeyup="lookup(this.value);" onblur="fill();" class="form-text" />

现在我如何跟jquery处理?

下面是jquery代码
<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
 function lookup(inputString) {
  if(inputString.length == 0) {
   // Hide the suggestion box.
   $('#suggestions').hide();
  } else {
   $.post("rpc.php", {queryString: ""+inputString+""}, function(data){  //rpc.php模糊查询搜索结果
    if(data.length >0) {
     $('#suggestions').show();
     $('#autoSuggestionsList').html(data);
    }
   });
  }
 } // lookup
 
 function fill(thisValue) {
  $('#inputString').val(thisValue);
  setTimeout("$('#suggestions').hide();", 200);
 }
</script>

论坛: