2013-08-20 8 views
6

में त्रुटि परिभाषित नहीं किया गया है, तो मैं दूरस्थ डेटा लोड करने के लिए select2 प्लगइन का उपयोग कर रहा हूं। मैं एक एएसपीएक्स पेज का उपयोग कर रहा हूं जो JSON डेटा देता है और इसे select2 प्लगइन को असाइन किया जाता है। उपयोगकर्ता चयन 2 टेक्स्टबॉक्स से कुछ मान चुनने के बाद, मैं पेज को पोस्टबैक करने के लिए मजबूर कर रहा हूं। पोस्टबैक के बाद मैं चयन 2 टेक्स्टबॉक्स में टेक्स्ट सेट करने के लिए पुनः लोड करने के लिए निम्न कोड का उपयोग कर रहा हूं।वैल() को कॉल नहीं कर सकता है अगर initSelection() को select2 प्लगइन

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

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

लेकिन सिस्टम निम्न त्रुटि फेंक है: cannot call val() if initSelection() is not defined

यहां तक ​​कि अगर मैं init परिभाषित करते हैं, मैं मान सेट करने में सक्षम नहीं हूँ। मैं निम्नलिखित कोड का उपयोग कर रहा हूँ। कृपया पोस्टबैक के बाद select2 टेक्स्टबॉक्स पर मान सेट करने में मेरी सहायता करें। !

$(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

'init चयन '' AJAX'-संपत्ति के अंदर नहीं होना चाहिए। – user1983983

उत्तर

5

आप कर गलती मैं इस समस्या का सामना करना पड़ा है और मैं तुम्हें देखा question.Then मैं Select2 और झरना के डी एपीआई डॉक्स कि मैं wrong.Pleas ajax.like इस के साथ initSelection समानांतर जगह था पढ़ें:

$("#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 
       }; 
      }, 
     }, 
    }); 
संबंधित मुद्दे