2016-07-25 21 views
0

Ich habe die folgende JavaScript Drop-Down-Liste zu füllen select2 mit https://select2.github.io/Erste select2 Daten zu aktualisieren Ajax auf Drop-Down-

Es funktioniert gut, und füllt die Liste auf der ersten Last der Seite. Ab diesem Zeitpunkt wird die Liste nicht aktualisiert, auch wenn Daten hinzugefügt werden, da sie nur einmal den AJAX-Aufruf durchführt. Auch wenn ich die Seite neu lade, wird die Dropdown-Liste nicht aktualisiert und der AJAX-Aufruf wird nicht ausgelöst (es sei denn, ich schließe und öffne den Browser, dann wird der AJAX-Aufruf ausgelöst)

Gibt es eine Möglichkeit, den Ajax-Aufruf jeweils durchzuführen Zeit das Dropdown geöffnet ist. Ich habe die Option .on ("select2-open") ausprobiert, hatte aber kein Glück.

Entschuldigung JavaScript ist nicht etwas, über das ich viel weiß.

$("#Location").select2({ 
      placeholder: "Select a known location", // Placeholder text 
      allowClear: true, //Allows deselection of chosen address 
      ajax: { 
       url: '/AlertInterface/NewAlertLocations', // Where we want the ajax to call 
       dataType: 'json', // The datatype we are expecting to be returned 
       type: "GET", //Just a get method 
       //Data: allows us to pass a parameter to the controller 
       data: function (query) { 
        console.log(query) 
        return { search: query.term } 
       }, 
       //processes the results from the JSON method and gives us the select list 
       processResults: function (data) { 
        console.log(data) 
        return { 
         results: JSON.parse(data) 
        }; 
       } 
      } 
     }); 

EDIT:

Ich versuche, habe

$("#Location").on("select2:open", function() { $("#Location").select2(); }) 

zu verwenden, aber das half nicht. :-(

Antwort

0

Sie haben einen Syntaxfehler in Ihrem Code Bitte überprüfen Sie den Code unten,

$("#Location").select2({ 
      placeholder: "Select a known location", // Placeholder text 
      allowClear: true, //Allows deselection of chosen address 
      ajax: { 
       url: '/AlertInterface/NewAlertLocations', // Where we want the ajax to call 
       dataType: 'json', // The datatype we are expecting to be returned 
       type: "GET", //Just a get method 
       //Data: allows us to pass a parameter to the controller 
       data: function (query) { 
        console.log(query) 
        return { search: query.term } 
       }, 
       //processes the results from the JSON method and gives us the select list 
       processResults: function (data) { 
        console.log(data) 
        return { 
         results: JSON.parse(data) 
        }; 
       } 
      } 
    }); 
+0

Die Sanitäter ich ein bisschen zu viel kopiert vergessen die extra}).!. – cramar

+0

Ist Ihre Seite live, wenn ja, bitte den Link angeben :) –