2013-08-20 23 views
6

Tôi đang sử dụng plugin select2 để tải dữ liệu từ xa. Tôi đang sử dụng một trang aspx trả về dữ liệu JSON và cùng được gán cho plugin select2. Sau khi người dùng chọn một số giá trị từ hộp văn bản select2, tôi đang buộc trang phải đăng lại. Sau khi postback tôi đang sử dụng mã sau đây để tải lại để thiết lập văn bản trong hộp văn bản select2.không thể gọi val() nếu initSelection() không được xác định lỗi trong plugin select2

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

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

Nhưng hệ thống được ném lỗi sau: cannot call val() if initSelection() is not defined

Thậm chí nếu tôi xác định init, tôi không thể để thiết lập giá trị. Tôi đang sử dụng mã sau đây. Hãy giúp tôi đặt giá trị trên hộp văn bản select2 sau khi postback. !

$(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' không được nằm trong thuộc tính' ajax'. – user1983983

Trả lời

5

bạn thực hiện sai lầm tôi đã phải đối mặt với vấn đề này và tôi thấy bạn question.Then tôi đọc tài liệu API de của Select2 và suối mà tôi đã được wrong.Pleas đặt song song với initSelection ajax.like này:

$("#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 
       }; 
      }, 
     }, 
    }); 
Các vấn đề liên quan