JQuery auto complete and form auto submit

2019-08-23 18:11发布

I'm using the JQuery auto complete ( jquery.autocomplete.js ). and I'm wondering how can I submit a form once an item has been selected? currently when an item is select you have to hit enter twice, once to get the item into the search field, and once to submit the search. I'd like to submit the search on the first enter or when an item selecting by mouse.

how can I use this to submit the form???? This is my code::

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

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

THis is the form::

<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>

标签: jquery forms
2条回答
太酷不给撩
2楼-- · 2019-08-23 18:35

You haven't posted your auto complete code, or your form code so I don't know how you expect anyone to tailor an answer for you. However, you simply need to bind something to the select event. This should do it:

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

Docs here.

查看更多
一纸荒年 Trace。
3楼-- · 2019-08-23 18:40

you can use select function like this, 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');

        }
    });
查看更多
登录 后发表回答