【问题标题】:Pass array as parameter to Select2将数组作为参数传递给 Select2
【发布时间】:2013-06-25 15:09:15
【问题描述】:

我正在尝试将选中的复选框放入一个数组 (checkBoxArray) 中,并使用 Select2 jQuery 插件通过 ajax 将其发送到我的 php 请求文件。 但 filter 值始终为空。

//Here the checkBox array
$('.chkfilters').on("change", function() {
    var checkBoxArray = $('.chkfilters:checked').map(function(i,n) {
        return $(n).val();
    }).get(); //get converts it to an array
});

// Here the Select2 plugin
$(".searchInput").select2({
    placeholder: "Rechercher un article, une référence...",
    minimumInputLength: 3,
    ajax: {
        url: "/suggest",
        dataType: 'JSONP',
        type: 'GET',
        data: function (term, page, checkBoxArray) {
            return {
                q: term,
                page_limit: 10,
                page: page,
                filter : checkBoxArray
            };
        },
        results: function (data, page) {
            var more = (page * 10) < data.total;
            return {
                results: data.articles,
                more: more
            };
        }
    },
    createSearchChoice: function(term, checkBoxArray) {
        return {
                text:term,
                tform:'/recherche',
                filter : checkBoxArray
         };
    },

    ...

});

【问题讨论】:

  • 您只是想获得选中的复选框吗?
  • 不仅如此。我也在尝试通过 Select2 函数发送数组。复选框在表单之外。
  • 你试过serialize吗?看看$('.chkfilters').serialize() 返回什么。 api.jquery.com/serialize
  • @AdamMerrifield 谢谢帮助。是的,这听起来是一个很好的起点,但你能帮我找到方法吗?

标签: php jquery jquery-select2


【解决方案1】:

我的项目中的代码。它有效:

$('.lstProgramStoplistId').select2({
  ajax: {
    url: programsUrl,
    dataType: 'json',
    contentType: 'application/json',
    type: 'POST',
    delay: 250,
    data: function (params) {
      var lstProgramCategoryId = $('.lstProgramCategoryId').select2('val');

      return JSON.stringify({
        str: params.term,
        lstProgramCategoryId: lstProgramCategoryId
      });
    },
    processResults: function (data, params) {
      return {
        results: data.items
      };
    },
    cache: false
  },
  minimumInputLength: 2
});

【讨论】:

    猜你喜欢
    • 2020-03-06
    • 2012-12-07
    • 1970-01-01
    • 2016-03-13
    • 2012-10-17
    • 2020-03-16
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多