【问题标题】:Select2 Multiple format to be passed to JSONSelect2 要传递给 JSON 的多种格式
【发布时间】:2016-04-03 13:51:04
【问题描述】:

我正在使用 select2 多个搜索框。我使用 JSON 传递这些数据并使用 ajax(JSON stringify) 保存它。

我只需要传递 2 个变量,即 ID(主键,自定义)和 Selection 本身。

当只选择了 1 个值时,我设法将其保存到数据库中。

选择多个值时,在我的 console.log 中,我看到类似这样的内容

{21,23,25,26}

这是选择本身。

我如何让它显示成这样,

Object0->{id:1, selection:21}
Object1->{id:2, selection:23}
Object2->{id:3, selection:25}
Object3->{id:4, selection:26}

下面是我正在使用的代码,

var nature = {
   ubtBusinessInfo: businessId, // the primary key
   ubtBusinessListing: nature.val() // here is selection
};

这里是select2的初始化,

    nature
    .select2({
        allowClear: true,
        placeholder: "Filter as you type",
        minimumInputLength: 3,
        multiple: true,
        ajax: {
            url: 'home/umkei/info/nature',
            dataType: 'json',
            quietMillis: 250,
            data: function (term, page) {
                return { q: term };
            },
            results: function (data, page) {
                return { results: data };
            },
            cache: true
        }
    })

自然是从定义的(我尝试了如下)

var nature = $('[name=nature_business]') OR var nature = $(#nature_business);

我知道它一定与 nature.val() 用法有关。一定是类似数组的东西,但我不知道如何将这些数据区分/拆分为键->值对。

谢谢。

【问题讨论】:

    标签: javascript jquery json jquery-select2 select2


    【解决方案1】:

    我大约在上周得到了这个,我想我会分享我的解决方案。

    var nature=[];
    var splitnature = nature_business.val().trim().split(',');
    var n;
    
    for(n=0; n<=splitnature.length-1;n++){
        nature.push({
        ubtBusinessListing: splitnature[n],
        ubtBusinessInfo: businessId
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多