简体   繁体   English

单击按钮时如何使用Instantsearch.js触发搜索

[英]How to fire a search with instantsearch.js when clicking on a button

I'm using Algolia instantsearch.js with a very simple search box and a search button like this http://codepen.io/anon/pen/aNXLBV 我将Algolia Instantsearch.js与一个非常简单的搜索框和一个类似http://codepen.io/anon/pen/aNXLBV的搜索按钮一起使用

I've changed the search box behavior with options.searchOnEnterKeyPressOnly , I'd like to have a search fired also when someone clicks on the Search button. 我已经使用options.searchOnEnterKeyPressOnly更改了搜索框的行为,当有人单击“ Search按钮时,我也希望触发Search

I'm sure this is a RTFM issue, but I couldn't find anything on this, neither in the Algolia general JS docs , also they look like two different beasts in some aspects. 我确定这是RTFM的问题,但是我在Algolia通用JS文档中都找不到任何东西,在某些方面它们看起来像是两个不同的野兽。

So how can I have a search fired both on Enter or clicking on the Search button? 那么如何在Enter或单击Search按钮上都触发Search

Suppose you have a button of id search-button and search box of id search-box 假设您有一个ID search-button和一个ID search-box

If you are useing jquery do like so.. 如果您正在使用jQuery,请这样做。

var search = instantsearch({
    appId: algoliaCredential.appId,
    apiKey: algoliaCredential.searcKey,
    indexName: 'INDEXNAME'
});

search.start();

$('#search-button').on('click', function(){
    var query = $('#search-box').val().trim();
    search.helper.setQuery(query).search(); //Set the query and search
})

I have the similar problem and i did that way. 我有类似的问题,我做到了。 Hope this will work. 希望这会起作用。

You can just bind your button click/submit and call: 您只需绑定按钮单击/提交并调用:

// search = instantsearch(); ...
search.helper.search();

Indeed this is not something trivial/documented and makes it hard to know when you only know instantsearch.js. 确实,这不是琐碎的/没有记录的事情,并且仅当您知道Instantsearch.js时就很难知道。

We will try to make it easier. 我们将尝试使其更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM