[Select2多选值在回发后不保留

问题描述 投票:0回答:2

我有一个问题,当单击提交或发生页面回发时,select2值不显示。我正在使用ajax助手从数据库加载数据。

这是我的select2代码:

$('#<%=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);
               }
        }
    });
asp.net jquery-select2
2个回答
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);
    });

0
投票

我相信这可能是由于您在回发期间没有重新填充SELECT中的项目(当您使用AJAX填充它们,并且它们不是模型的一部分。)因此,您试图设置SELECT的值到不存在的OPTION的ID。

© www.soinside.com 2019 - 2024. All rights reserved.