【发布时间】:2015-01-12 09:54:04
【问题描述】:
我有一个使用 C#、jQuery 和 Select2 v3.5.2 以及 Bootstrap 2.3.2 的 MVC5 应用程序。
现在我是 select2 的新手,所以我遇到了一些麻烦。例如,我想要一个包含多个项目的文本框,如标记支持示例所示。
现在,这应该不难,但问题是我需要在对服务器进行 Ajax 调用之后,使用 jQuery 在 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
});
这里是 Modal(主要是样板代码):
<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 中做错了什么?
【问题讨论】:
-
在我对 select2 的实验中,我总是使用
-
选择标签适用于 4.0 版。这是 3.5.2 版:P
标签: c# jquery ajax twitter-bootstrap jquery-select2