2013-08-20 7 views
6

リモートデータを読み込むためにselect2プラグインを使用しています。 JSONデータを返すaspxページを使用していて、select2プラグインにも同じものが割り当てられています。ユーザーがselect2テキストボックスから値を選択すると、私はページをポストバックに強制しています。ポストバックの後、次のコードを使用してselect2テキストボックスにテキストを設定し直します。は、initSelection()が定義されていないとval()を呼び出せません。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

'initSelection'は' ajax'プロパティの中にあってはなりません。 – user1983983

答えて

5

入力を間違えた私はこの問題に直面していると私はwrong.Pleasがajax.likeこれとinitSelection平行に配置されたSelectセレクトと泉のデAPIドキュメントを読んquestion.Then私はあなたを見た:

$("#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 
       }; 
      }, 
     }, 
    }); 
関連する問題