2014-01-22 26 views
0

我在點擊提交或頁面回發時沒有顯示select2值時出現問題。我使用ajax helper從數據庫加載數據。Select2多選的值在回發後不被保留

這裏是我的選擇2碼:

$('#<%=fstName.ClientID%>').select2({ 
     placeholder: 'Enter Reviewer', 
     minimumInputLength: 0, 
     multiple: true, 
     autoClear:false, 
     ajax: { 
      type: 'POST', 
      url: 'BulkPickers/GetRev', 
      dataType: 'json', 

      data: 
      function (term, page) { 
       return { 
        appinstid: appInstId, 
        searchTerm: term, 
        selection: $('#<%=fstName.ClientID%>').val() 
       }; 
      }, 
      results: function (data, page) { 

       var myResults = []; 
       $.each(data, function (index, item) { 
        myResults.push({ 
         id: item.id, 
         text: item.text 
        }); 
       }); 

       return { 
        results: myResults 
       }; 
      }, 
      initSelection: 
       function (element, callback) { 
        var data = []; 
        $(element.val().split(",")).each(function() { 
         data.push({ id: this, text: this }); 
        }); 
        callback(data); 
       } 
     } 
    }); 

回答

1

我找到了解決辦法。需要在文本更改時填充隱藏字段值,然後使用select2上的隱藏輸入值進行初始化。以下是完整的代碼:

$j('#<%=txtVendor.ClientID%>').select2({ 

     placeholder: 'type the name ', 
     minimumInputLength: 0, 
     multiple: true, 
     autoClear:false, 
     ajax: { 
      type: 'POST', 
      url: 'BulkPickers/GetVendor', 
      dataType: 'json', 

      data: 
      function (term, page) { 
       return { 
        appinstid: appInstId, 
        searchTerm: term, 
        selection: $j('#<%=txtVendor.ClientID%>').val() 
       }; 
      }, 
      results: function (data, page) { 

       var myResults = []; 
       $j.each(data, function (index, item) { 
        myResults.push({ 
         id: item.id, 
         text: item.text 
        }); 
       }); 

       return { 
        results: myResults 
       }; 
      } 
     } 
    }).select2("data",vendobj); 

    $j('#<%=txtVendor.ClientID%>').change(function() { 
     var selections = (JSON.stringify($j('#<%=txtVendor.ClientID%>').select2('data'))); 
     $j('#<%=hdnVend.ClientID%>').attr("value", selections); 
    });