/ / JSON + PHP + JQuery + Problema di completamento automatico - javascript, jquery, json

JSON + PHP + JQuery + Problema di completamento automatico - javascript, jquery, json

Ho iniziato solo oggi, ma sto diventando enormeproblemi nel cercare di capire JSON / AJAX, ecc., ho ottenuto il mio codice fino a questo punto, ma sono impantanato su come restituire i dati che vengono trascinati dalla richiesta AJAX alla funzione jQuery Auto completa.

var autocomplete = new function (){
this.init = function() {
$("#insurance_destination").autocomplete({source: lookup});
}

function lookup(){
$.ajax({
url: "scripts/php/autocomplete.php",
data: {query:this.term},
dataType: "json",
cache : false,
success: function(data) {
for(key in data){
return {
label: key,
value: data[key][0]
}
}
}
});
}

}

E un esempio della stringa JSON restituita da uno script PHP {"Uganda": ["UGA", "UK4", "Tutto il mondo escluso USA, Canada e Caraibi"]}

risposte:

1 per risposta № 1

Normalmente, non devi fare una query jax tu stesso:

$("#insurance_destination").autocomplete("url_here", {options_here});

Questo presume che stiamo parlando del plugin standard di completamento automatico jQuery. Ti ho capito bene?

modificare Controlla api
http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions
Ci sono anche alcuni esempi.


0 per risposta № 2

Questo è il codice con cui ho finito, funziona in Chrome e Firefox ma non in IE 6/7 ...

var autocomplete = new function (){
this.init = function() {
$("#insurance_destination").autocomplete({
source: function(request, response) {
debugger;
$.ajax({
url: "scripts/php/autocomplete.php",
dataType: "json",
data: {query:this.term},
success: function(data) {
response($.map(data.countries, function(item) {
return {
label: "<strong>"+item.name+"</strong>"+" "+item.description,
value: item.name,
code : item.region
}
}))
}
})
},
minLength: 2,
select: function(event, ui) {
$("#insurance_iso_code_hidden").val(ui.item.code);
},
open: function() {
},
close: function() {

}
});
}


}