2015-04-27 78 views
2

JS:搜索自动完成功能,通过导轨4

<script> 

$(function() { 
    var availableTags = [ 
     "NSDL", 
     "CDSL", 
     "ROCA", 
     "NSE", 
     "SEBI"  
    ]; 
    $("#search").autocomplete({ 
     source: availableTags 
    }); 
    }); 

</script> 

<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 

<link rel="stylesheet" href="/resources/demos/style.css"> 

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 

_index.js.erb

<%= form_tag service_requests_path, method: :get,:id=>"search_form" do %> 
<%= text_field_tag :search , nil, class: 'form-control input-breathe', required: true, placeholder: 'Searching for...', :autocomplete=> :on%><%end%> 

我想只用文本框进行搜索,而无需使用任何button.Now我从让所有的值文本框。但问题是,当我选择任何标签时,它应该搜索包含此标签的所有服务请求。我怎样才能做到这一点?

回答

2

你只需要添加事件监听器来形式,以便每当用户选择任何标签时,表单应该被自动提交。

var availableTags = [ 
    "NSDL", 
    "CDSL", 
    "ROCA", 
    "NSE", 
    "SEBI"  
]; 
$("#search").autocomplete({ 
    source: availableTags, 
    select: function(event, ui){ 
     $('#search').val(ui.item.value) 
     $('#search_form').submit() 
    } 
}); 

jquery自动完成插件提供选择回调,每选择标记时自动调用它。因此请提交您的搜索表单。我假设你的搜索表单有id'search_form'。请用您申请的身份证件替换它。

+0

它,不工作。我已经编辑了我的代码上面。 –

+0

您是否将ID添加到搜索表单中? –

+0

感谢它的工作。 –