2012-05-18 76 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