2013-08-20 15 views
6

Uzak veriyi yüklemek için select2 eklentisini kullanıyorum. JSON verilerini döndüren ve select2 eklentisine atanan bir aspx sayfası kullanıyorum. Kullanıcı select2 metin kutusundan bir değer seçtikten sonra sayfayı geri göndermeye zorluyorum. Geri gönderme işleminden sonra, select2 metin kutusundaki metni yeniden yüklemek için aşağıdaki kodu kullanıyorum., initSelection() select2 eklentisinde hata tanımlanmamışsa val() öğesini çağıramıyor

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

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

Ama sistem şu hatayı atıyor: cannot call val() if initSelection() is not defined

ben init tanımlamak bile, değerini ayarlamak mümkün değilim. Aşağıdaki kodu kullanıyorum. Lütfen geri gönderme işleminden sonra select2 metin kutusundaki değeri ayarlamama yardım edin. !

$(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',' ajax' özelliği içinde olmamalıdır. – user1983983

cevap

5

Yaptığınız hata i bu sorunla karşı karşıya gelmiş ve ben wrong.Pleas ajax.like bununla initSelection paralel yerleştirin edildi 2'yi Seç ve memba de API docs okumak question.Then seni gördüğüm:

$("#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 
       }; 
      }, 
     }, 
    });