2015-01-12 48 views
0

我有一個使用C#,jQuery和Select2 v3.5.2的MVC5應用程序,以及Bootstrap 2.3.2。Select2沒有更新條目

現在我是新來的select2,所以我有一些麻煩。例如,我想要一個包含多個項目的文本框,如標記支持的示例中所示。

現在,這不應該很難,但問題是我需要使用jQuery在向服務器發出Ajax調用之後以Bootstrap模式動態更新這些值。

聽起來很複雜,但很簡單。當用戶點擊一個按鈕來編輯項目,我展現出Bootsrap模式,與該項目的信息,查詢服務器上該項目的信息後:

//Show Edit Package modal 
    $("a.btn.btn-default.editPackage").click(function() { 
     //ask the server for item information 
     $.ajax({ 
      url: $(this).data('url'), 
      type: 'get', 
      cache: false, 
      success: function (result) { 
       //this input field belongs to the modal 
       $(input #SubCategoryId).attr('data-edit-values', '13 - Orange'); 
       $('#myModal').show(); 
      } 
     }); 
     return false; //prevent browser defualt behavior 
    }); 

這裏是模態(主要是鍋爐板代碼):

<div class="modal fade" id="@Model.modalId" tabindex="-1" role="dialog" aria-labelledby="@Model.modalId" aria-hidden="true"> 
    <div class="modal-dialog" style="overflow-y: auto; max-height: 80vh;"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <h4 class="modal-title" id="myModalLabel">@Model.modalTitle</h4> 
      </div> 
      <div class="modal-body" data-val="@Model.packageId"> 
       <form class="form-horizontal"> 

        <div class="control-group"> 
         <label class="control-label">Select the Function:</label> 
         <div class="controls"> 
          <input class="select2s" data-edit-values="@Model.functionsString" data-o2f-placeholder="Select Employee Function..." data-o2f-url="@Url.Action("FunctionsMultiple", "Home")" data-val="false" id="SubCategoryId" name="SubCategoryId" multiple="multiple" type="text"/> 
         </div> 
        </div> 

       </form> 
      </div> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
       <button type="button" class="btn btn-primary" id="@Model.saveButtonId" data-url="@Model.saveButtonUrl">@Model.saveButtonText</button> 
      </div> 
     </div> 
    </div> 
</div> 

的問題是,當我加載彈出,不裝載在字段中的值。

我在做什麼錯誤在我的javaScript?

+0

隨着我的實驗與選擇2,我總是用一個。 – Jerry

+0

選擇標籤用於版本4.0。這是版本3.5.2:P –

回答

0

要更新任何模式一個選擇2字段的值,我意識到我不得不使用一個特殊的函數從選擇2的效果:$('#fieldId').select2('data', "blablablaMyData");