2016-05-13 4 views
0

Ich erstelle eine Tabelle mit einer Schaltfläche. Wenn ein Benutzer den Button wechselt, öffnet sich ein Bootstrap-Modal. Innerhalb des Modal ist ein Formular mit einem getippten Auswahlfeld. Die Liste funktioniert und alle Elemente sind wählbar.Typeahead in einem Modell geben leeren Wert zurück, um Eingabefeld zu bilden

Mein Problem: Wenn ich ein Element auswähle, zeigt es auf Formular auf Post der Rückgabewert (PHP $ _POST) sind leer.

Mein js Code

$(document).ready(function() { 
$('.typeahead').typeahead({ 
    source: function (query, process) { 
     $.ajax({ 
      url: '/testPost', 
      type: 'POST', 
      dataType: 'JSON', 
      //data: 'query=' + query, 
      data: 'query=' + $('#contactName').val(), 

      success: function(data) 
      { 
       var results = data.map(function(item) { 
        //var someItem = { contactname: item.ContactName, telephone: item.Telephone, email: item.Email }; 
        var someItem = { contactname: item.ContactName, id: item.id }; 
        console.log(item); 
        return JSON.stringify(someItem.contactname); 
       }); 

       return process(results); 
      } 
     }); 
    }, 
    minLength: 1, 
    updater: function(item) { 
     // This may need some tweaks as it has not been tested 
     var obj = JSON.parse(item); 
     return item; 
    } 
}); 

});

php json

$return_arr[] = array("id" => "1", "ContactName" => "Dooome"); 
$return_arr[] = array("id" => "2", "ContactName" => "Caa"); 
$return_arr[] = array("id" => "3", "ContactName" => "Veee"); 
$return_arr[] = array("id" => "4", "ContactName" => "Moo"); 
$return_arr[] = array("id" => "5", "ContactName" => "Reee"); 
#$return_arr[] = array("value" => "Dooome"); 

header("Content-Type: application/json"); 
print_r(json_encode($return_arr)); 

Antwort

0

Wenn Sie in typeahead Eingang auswählen möchten .val() nicht funktioniert, Sie Wert erhalten mit: $('#contactName').typeahead('val'), auf diese Weise Sie Wert erhalten der Eingang für

Ajax in Daten des Eintrages Senden

Schauen Sie sich the documentation of typeahead

Verwandte Themen