1

I recently implemented a search box in my site and it gives good result with auto complete of keywords. I want to implement a hidden part to know what the search queries are that my registered visitors searched for.

I added a hidden username in the search box and when the user searches for anything, the name and search queries are saved in another table. Because of auto complete, the data is saved every second; so if someone searches for "Laptop", we get:

**ID  username  Searchdata srtime**  
1    user1234   L  1372513199            
2    user1234   La 1372513200  
3    user1234   Lap 1372513201   
4    user1234   Lapt 1372513202   
5    user1234   Lapto 1372513203  
6    user1234   Laptop 1372513204

jQuery code:

$('#search_box').keyup(function() { // Other things });

How can I save only the final search term and ignore the auto complete values?

4

2 回答 2

1

您应该使用.blur()而不是.keyup().

这样,当控件失去焦点时提交数据,而不是每次按下键时提交。

于 2013-07-01T14:39:37.997 回答
0

只需按照以下步骤操作。

  1. 将数据保存在隐藏字段中
  2. 更新 keyup 上的隐藏字段。
  3. 当用户从搜索字段中聚焦时保存查询。

Javascript代码

$('#search').keyup(function(){  
    $('#searchQuery').val($('#search').val());
});
$('#search').focusout(function(){  
     alert($('#searchQuery').val()); //this will return final query
 });

html代码

<input type="text" id="search">
<input type="hidden" id="searchQuery">
于 2013-07-01T15:14:46.833 回答