2013-08-20 11 views
6

Używam wtyczki select2 do ładowania danych zdalnych. Używam strony aspx, która zwraca dane JSON, a ta sama jest przypisana do wtyczki select2. Po wybraniu przez użytkownika wartości z pola tekstowego select2 zmuszam stronę do odświeżenia strony. Po odświeżeniu strony używam następującego kodu do przeładowania, aby ustawić tekst w polu tekstowym select2.nie można wywołać val(), jeśli initSelection() nie jest zdefiniowany błąd w wtyczce select2

var data = { "PatientID": "XYX", "Email": "[email protected]" }; 

      $('#e6').select2('val', '123'); 

Ale system jest rzucanie następujący błąd: cannot call val() if initSelection() is not defined

Nawet gdybym określić startowych, nie jestem w stanie ustawić wartość. Używam następującego kodu. Proszę mi pomóc ustawić wartość w polu tekstowym select2 po odświeżeniu strony. !

$(document).ready(function() { 
     $("#e6").select2({ 
      placeholder: "Search for a movie", 
      minimumInputLength: 1, 
      ajax: { // instead of writing the function to execute the request we use Select2's convenient helper 
       url: "data.aspx", 
       dataType: 'json', 
       quietMillis: 1000, 
       data: function (term, page) { 
        return { 
         name: term 
        }; 
       }, 
       initSelection: function (element, callback) { 
        var data = { "PatientID": "XYX", "Email": "[email protected]" }; 
        callback(data); 
       }, 

       results: function (data) { 
        var results = []; 
        $.each(data, function (index, item) { 
         results.push({ 
          id: item['Email'], 
          text: item['PatientID'] 
         }); 
        }); 
        return { 
         results: results 
        }; 
       }, 
      }, 
     }); 

    }); 

    window.onload = function() { 
     var data = { "PatientID": "XYX", "Email": "[email protected]" }; 
     //When this is called system is throwing error 
     //This code is required to show the value in select2 textbox after the post back 
     $('#e6').select2('val', data); 
    } 

    $(document).ready(function() { 
     $("#e6").on("select2-selecting", function (e) { 
      //alert("selecting val=" + e.val + " choice=" + JSON.stringify(e.choice)); 
      var id = document.getElementById('<%= savebtn.ClientID %>'); 
      document.getElementById('<%= hdnFld.ClientID %>').value = e.val; 
      id.value = e.val; 
      //causes post back 
      id.click(); 

     }); 
    }); 
+4

'initSelection' nie powinno znajdować się wewnątrz właściwości' ajax'. – user1983983

Odpowiedz

5

zrobić błąd mam w obliczu tego problemu i widziałem cię question.Then czytałem de docs API Select2 i czcionka, że ​​mi wrong.Pleas umieścić równolegle initSelection z ajax.like to:

$("#e6").select2({ 
     placeholder: "Search for a movie", 
     minimumInputLength: 1, 
     initSelection: function (element, callback) { 
       var data = { "PatientID": "XYX", "Email": "[email protected]" }; 
       callback(data); 
      }, 
     ajax: { // instead of writing the function to execute the request we use Select2's convenient helper 
      url: "data.aspx", 
      dataType: 'json', 
      quietMillis: 1000, 
      data: function (term, page) { 
       return { 
        name: term 
       }; 
      }, 


      results: function (data) { 
       var results = []; 
       $.each(data, function (index, item) { 
        results.push({ 
         id: item['Email'], 
         text: item['PatientID'] 
        }); 
       }); 
       return { 
        results: results 
       }; 
      }, 
     }, 
    }); 
Powiązane problemy