2015-04-27 103 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

感謝它的工作。 –