2012-05-18 75 views
0

我正在使用JQuery自動完成(jquery.autocomplete.js)。 我想知道如何在提交表單後選擇一個項目? 當前當某個物品被選中時,您必須按兩次輸入,一次到 將物品放入搜索字段,然後一次提交搜索。我想 喜歡提交搜索的第一個輸入或當一個項目選擇鼠標。JQuery自動完成和表單自動提交

我該如何使用它來提交表格? 這是我的代碼::

$(function() { 
    $("#artist").autocomplete({ 
    source: function(request, response) { 
      $.ajax({ 
       url: "http://", 
       dataType: "jsonp", 
       data: { 
        results: 12, 

        format:"jsonp", 
        name:request.term 
       }, 

這是形式::

<form id="mainSearchForm" onSubmit="ytvbp.listVideos(this.queryType.value, this.searchTerm.value, 1); document.forms.searchForm.searchTerm.value=this.searchTerm.value; ytvbp.hideMainSearch(); document.forms.searchForm.queryType.selectedIndex=this.queryType.selectedIndex; return false;"> 
     <div align="left"> 
     <select name="queryType" onChange="ytvbp.queryTypeChanged(this.value, this.form.searchTerm);"> 
      <option value="all" selected="true">All Videos</option> 
      <option value="top_rated">Top Rated Videos</option> 
      <option value="most_viewed">Most Viewed Videos</option> 
      <option value="recently_featured">Recently Featured Videos</option> 

     </select> 
     <input name="searchTerm" id="artist" > 
     <input type="submit" value="Search"> 
     </div> 
     </form> 
+0

安置自己的自動完成代碼 – Dunhamzzz

+0

你可以用「平變化」,並提交表單。 – Dev

回答

0

你可以使用這樣的選擇功能,Select Event in Autocomplete

$("#yourID").autocomplete({ 
     source: "YourUrl.php", 
     minLength: 2, 
     select: function(event, ui) { 
      log(ui.item ? 
       "Selected: " + ui.item.value + " aka " + ui.item.id : 
       "Nothing selected, input was " + this.value); 
          // you can replace this and put the submit code. by calling submit() method. 
         $(this).closest('form').trigger('submit'); 

     } 
    }); 
4

您還沒有發佈你的汽車完整的代碼或你的表格代碼,所以我不知道你期望任何人爲你定製一個答案。但是,您只需要將某些內容綁定到select事件。這應做到:

$('#whatever').autocomplete({ 
    select: function() { 
     $(this).closest('form').trigger('submit'); 
    } 
}); 

Docs here.

+0

我已經輸入上面的代碼。 – Rachi

+0

@Rachi你只發布了一半你的javascript,你錯過了結束標籤,只是試圖實現我已經放入你當前的設置。 – Dunhamzzz