【问题标题】:Select2: set hidden field valueSelect2:设置隐藏字段值
【发布时间】:2013-05-22 08:20:41
【问题描述】:

我有以下代码:

<input type="hidden" name="airport" tabindex="6" />


$('input[name="airport"]', '#details-form').select2({
            minimumInputLength: 3,
            multiple: true,
            separator: ';',
            width: 'off',
            initSelection : function (element, callback) {
                var data = [];
                $(element.val().split(';')).each(function () {
                    data.push({id: this, text: this});
                });
                callback(data);
            },
            ajax: { 
                url: History.getBasePageUrl() + 'get-airports-dictionary',
                dataType: 'json',
                data: function (term, page) {
                    return {
                        q: term
                    };
                },
                results: function (data, page) {
                    var code = parseInt(data[0]);
                    switch (code) {
                        case 0: 
                            return {results: data[1]};
                            break;
                        default:
                            window.Common.showError(data[1]);
                            break;
                    }
                }
            },
            formatResult: formatAirportResult,
            formatSelection: formatAirportSelection
        });

一切正常,直到我想为该字段设置现有值 - initSelection 方法中的元素参数显示空值!

$('input[name="airport"]', '#details-form').select2('val', '2');

我尝试使用以下代码设置原始值:

$('input[name="airport"]', '#details-form').val('2');

它可以工作,但是在 select2 的 val 方法中的某个地方,值消失了..

我使用 Select2 的 3.4.0 版和 this update

谢谢

【问题讨论】:

标签: jquery jquery-plugins jquery-select2


【解决方案1】:

这是可行的解决方案:

$('input[name="airport"]', '#details-form').select2({
            minimumInputLength: 3,
            multiple: true,
            separator: ';',
            width: 'off',
            initSelection : function (element, callback) {
                var data = [];
                $(element.val().split(',')).each(function(i) {
                    var item = this.split(':');
                    data.push({
                        id: item[0],
                        label: item[1]
                    });
                });
                callback(data);
            },
            ajax: { 
                url: History.getBasePageUrl() + 'get-airports-dictionary',
                dataType: 'json',
                data: function (term, page) {
                    return {
                        q: term
                    };
                },
                results: function (data, page) {
                    var code = parseInt(data[0]);
                    switch (code) {
                        case 0: 
                            return {results: data[1]};
                            break;
                        default:
                            window.Common.showError(data[1]);
                            break;
                    }
                }
            },
            formatResult: formatAirportResult,
            formatSelection: formatAirportSelection
        });
        function formatAirportResult(item) {
            return item.label + ' <strong class="pull-right">' + item.code + '</strong>';
        }
        function formatAirportSelection(item) {
            return item.label;
        }

我不知道为什么,但是 value 参数不能是字符串 - 为了使它工作,它必须是一个字符串数组。否则 select2 将空值设置为输入字段。

$('input[name="airport"]', '#details-form').select2('val', ['2:Domodedovo', '3:Vnukovo']);

证明 - http://jsfiddle.net/GZyeb/1/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2019-03-23
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多