2017-10-25 3 views
0

Ich benutze einen Jquery Autocomplete:jquery die automatische Vervollständigung, Syntaxanalyse Daten

die Seite, was auch immer zurück:

Array [ Object, Object, Object, Object ] 

Objekt hat id: INTEGER und Wert: STRING

ele.autocomplete({ 
    minLength: 1,    
    delay: 150,    
    source: function(request, response) { 
     jQuery.ajax({ 
      url: "whatever", 
      data: {       
       term: request.term 
      }, 
      dataType: "json", 
      success: function(data) { 
       response(data);             

       /* this actually works id is correctly logged 
       data.forEach(function(element) { 
        console.log(element.id); 
       });*/ 


      } 
     }); 
    },    
    select: function(e, ui) { 
     //do i have to do something here? (i guess) 
    } 
}); 

Was ich will erreichen (und ich bin dazu nicht in der Lage): Wenn ich einen Wert aus dem Autocomplete-Menü auswähle, wird der Eingabewert (ele) in die entsprechende ID geändert, während "text" der Zeichenfolgenwert bleibt.

Was kann ich tun? Vielen Dank im Voraus

Antwort

0

Ok, nur für die Zukunft, wenn jemand braucht es:

response(data); is not ok 

response($.map(data, function(item) { 
     return { 
     value: item.id, //set the value property with id integer 
     label: item.value //set the label property with "value" string 
     }; 
})); 

dann

select: function(e, ui) {  //when selecting an element from the list 
    var keyid = ui.item.value; //get value wich now contains id 
    var keylab = ui.item.label; //and the label wich contains the string 
    console.log(keyid + ' - ' + keylab); //do what you have to do 
} 
Verwandte Themen