我有以下搜索表格:
<%= form_tag search_path, method: "get", id: "other-form" do %>
<div class="input-group">
<%= text_field_tag :address, params[:address], id:"address",
class: "address", placeholder: "Enter address" %>
<button class="btn btn-custom" type="submit"><i class="glyphicon glyphicon-search"></i></button>
<%= text_field_tag :search, params[:search], class: "search",
placeholder: "Enter keyword" %>
这是自动完成-控制器内部的提前动作:
def autocomplete
if params[:search].present?
render json: Item.search(params[:search], {
fields: ["title"],
match: :word_start,
limit: 10,
load: false,
misspellings: {below: 5}
}).map(&:title).uniq
else params[:search_other].present?
render json: Item.search(params[:search], {
fields: ["title"],
match: :word_start,
limit: 10,
load: false,
misspellings: {below: 5}
}).map(&:title).uniq
end
end
这是它的javascript:
<script>
var items = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/items/autocomplete?search=%SEARCH',
wildcard: '%SEARCH'
}
});
$('#search')
.typeahead(null, {source: items})
.on('typeahead:selected', function(e){
})
</script>
我需要的设置是:搜索之后,当我回击时,将所有以前的值保留在表单中。目前,当我回击时,显示的是地址值,但没有显示“输入关键字”。现在,如果我摆脱了这一点:
$('#search')
.typeahead(null, {source: items})
.on('typeahead:selected', function(e){
})
在我按下后,将出现“输入关键字”值,但之后不是我要的设置。关于如何实现这一点的任何想法?