如何在Meteor中实现实时搜索,每个字母后没有文字输入模糊? [英] How to implement a live search in Meteor without text input blurring after every letter?

查看:71
本文介绍了如何在Meteor中实现实时搜索,每个字母后没有文字输入模糊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个基本的实时搜索栏,所以我有一个事件听取任何键盘:

I am trying to implement a basic live search bar so I have an event listening to any keyup:

'keyup input.search-query': function (evt) {
    Session.set("search-query", evt.currentTarget.value);
}, 

这似乎有效,除非我按任意键,搜索字段模糊所以我输入的第二个字母不在文本字段中!任何想法如何防止这种情况发生?

This seems to be working, except anytime I press a key, the search field is blurred so the second letter I type isnt in the text field! Any ideas how to prevent this from happening?

编辑:

更多信息:

Template.search.events({
    'keyup input.search-query': function (evt) {
        Session.set("search-query", evt.currentTarget.value);
    }, 
})

Template.search.searchResults = function () {
    var keyword  = Session.get("search-query");
    var query = new RegExp( keyword, 'i' );
    var results = Articles.find( { $or: [{'user': query},
                                        {'title': query},
                                        {'articleText': query},
                                        {'datetime': query}] } );
    return {results: results};
}


推荐答案

问题在于HTML其实。

The problem was in the HTML actually.

我将搜索栏和搜索结果放在同一个模板中,因此每当模板刷新新结果时,它都会模糊搜索栏。我所做的只是将搜索结果作为自己的模板并在搜索栏下方导入该模板,所以一切看起来都一样。

I has the search bar and the search results in the same template, so whenever the template would refresh with new results, it would blur the search bar. All I did was make the search results its own template and import that template below the search bar so everything looks the same.

这篇关于如何在Meteor中实现实时搜索,每个字母后没有文字输入模糊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆