Jquery Autocomplete AJAX call for label value pair JSON data SOLVED -


this jquery autocomplete code not working dynamic data. having json data label : contact name , value : contact id autocomplete doesnt work ajax call.

$("#autocomplete2").autocomplete({     //source: data,      source: function (request, response) {         $.ajax({             type: "post",             url: "http://localhost/leadata.php",             datatype: "json",             data: { q: request.term },             success: function (data) {                 var transformed = $.map(data, function (el) {                     return {                         label: el.label,                         value: el.value                     };                 });                 alert(transformed);                 response(transformed);             },             error: function () {                 response([]);             }         });     },     response: function (event, ui) {         if (ui.content.length === 0) {             $("#empty-message").text("no results found");         } else {             $("#empty-message").empty();         }     },     focus: function (event, ui) {         event.preventdefault();         $(this).val(ui.item.label);     },     select: function (event, ui) {         event.preventdefault();         $(this).val(ui.item.label);         $("#autocomplete2-value").val(ui.item.value);         alert(ui.item.value);         //$('.college').trigger('click');     } }); 

<!doctype html> <html lang="en"> <head>   <meta charset="utf-8">   <title>jquery ui autocomplete - remote datasource</title>   <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">   <script src="//code.jquery.com/jquery-1.10.2.js"></script>   <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>   <link rel="stylesheet" href="/resources/demos/style.css">   <style>   .ui-autocomplete-loading {     background: white url("images/ui-anim_basic_16x16.gif") right center no-repeat;   }   </style> <script> $(function() {     $( "#birds" ).autocomplete({         source: "http://localhost/autocomplete/test.php",         minlength: 2,         response: function(event, ui) {             if (ui.content.length === 0) {                 $("#empty-message").text("+ add contact");             } else {                 $("#empty-message").empty();             }         },         focus: function(event, ui) {                                 event.preventdefault();             $(this).val(ui.item.label);         },         select: function( event, ui ) {             /*               log( ui.item ?             "selected: " + ui.item.value + " aka " + ui.item.id :             "nothing selected, input " + this.value );             */             event.preventdefault();             //$(this).val(ui.item.label);              $("#birdsval").val(ui.item.label);             $("#birdsid").val(ui.item.value);             //$('.college').trigger('click');             $(this).val('');          },      }); }); </script> </head> <body>  <div class="ui-widget">     <label for="birds">birds: </label>     <input id="birds"><br />     <input id="birdsid">     <input id="birdsval"> </div>  <div id="empty-message"></div>  </body> </html> 

Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

html - grunt SVG to webfont -

c# - SharpSsh Command Execution -