weixin_33734785 2015-01-12 09:54 采纳率: 0%
浏览 72

Select2不更新条目

I have a MVC5 application using C#, jQuery and Select2 v3.5.2, together with Bootstrap 2.3.2.

Now I am new to the select2, so I am having some trouble. For example, I want a text box with multiple items, as seen in the example of Tagging support.

Now, this shouldn't be hard, but the problem is that I need to update those values dynamically in a Bootstrap modal, using jQuery, after making an Ajax call to the server.

It sounds complex, but it is simple. When a user clicks a button to Edit an item, I show a Bootsrap modal, with the information of that item, after querying the server for the information on that item:

 //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
    });

And here is the Modal (mostly boiler plate code):

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

The problem is that when I load the popup, the values on the field are not loaded.

What am I doing wrong in my javaScript?

  • 写回答

1条回答 默认 最新

  • weixin_33736832 2015-06-03 10:33
    关注

    To update the value of a select2 field in any modal I realized I had to use a special function from select2 for the effect: $('#fieldId').select2('data', "blablablaMyData");

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程