2 steps Ajax calls in one autocomplete field

In our code, sometimes we need update drop down list depends on the content in another field entered. Below is an example to show how to use autocomplete field, after you selected the item, and triggered drop down list option selected.


$(document).ready(function(){
	var cache = {};
	$( "#lookup_ticketnum" ).autocomplete({
	  source: function( request, response ) {		  
	    var term = request.term;
	    if ( term in cache ) {
	      response( cache[ term ] );
	      return;
	    }
	 
	    $.getJSON( "/matrix/ajax_search_cm.php", request, function( data, status, xhr ) {	    	
	      cache[ term ] = data;
	      response( data );
	    });
	  },
  	  minLength: 3,
  	  select: function( event, ui ) {
  		//post ajax call to update cm group perform.
		$.ajax({
		 url:'/matrix/ajax_search_cm_group_perform.php',
	     type:'post',
	     data:{term:ui.item.value},
	     success: function( msg ) {
		    $('#entry_cm_group_perform').val(msg);
		    $($("#entry_cm_group_perform option")[msg]).attr('selected','selected');									    											    
	     },
         error: function() {
		 	alert("Something went wrong, please try again or contact a system administrator if the problem persists.");
         }
		});	  									  		  
      }    
    });										
});

Advertisements

About qianggan

Sr. Software Engineer
This entry was posted in Computers and Internet, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s