【问题标题】:Select2 ajax is executed 2 timesSelect2 ajax 执行 2 次
【发布时间】:2015-09-01 10:37:26
【问题描述】:

我有一个从后端获取一些数据并填充 select2 下拉列表的脚本。问题是ajax调用总是被调用2次,它不应该是这样的。我不确定我做错了什么......任何帮助都会受到赞赏。

这是我的代码:

var select2Element = $('select').select2({
    theme: "classic",
    escapeMarkup: function (markup) { return markup; },
});

select2Element.on('select2:opening', function(event) {
    var clicked = $(this);

    var route = "{{ path('get_attribute_list', {'articleId': 'ARTICLEID', 'attributeGroupId': 'ATTRIBUTEGROUPID'}) }}"
    var url = route.replace("ARTICLEID", $(this).attr('data-articleId')).replace('ATTRIBUTEGROUPID', $(this).attr("data-attributeGroupId"));

    $.ajax ({
        url: url,
        dataType: 'json',
        async: false,
        type: "GET",
    }).then(function (data) {

        //@TODO get out elements already inserted

        for (var d = 0; d < data.length; d++)
        {
            var item = data[d];

            // Create the DOM option that is pre-selected by default
            var option = new Option(item.text, item.id, true, true);

            // Append it to the select
            clicked.append(option);
        }
        // Update the selected options that are displayed
        clicked.trigger('change');
    });

});

var inputResult = [];
select2Element.on('select2:select', function(e) {
    var jsonValue = {
        "articleId": $(this).attr("data-articleId"),
        "attributeGroupId": $(this).attr("data-attributeGroupId"),
        "attributeId": e.params.data.id
    }
    inputResult.push(jsonValue);

    $('#addAttributes').val(JSON.stringify(inputResult));
});

select2Element.on('select2:close', function() {
    $(this).html('');
});

【问题讨论】:

    标签: javascript jquery ajax jquery-select2 ui-select2


    【解决方案1】:

    “select2:open”和“select2:opening”中似乎存在错误。对此有修复但未发布。 无论如何,在修复之前遇到此问题的人可以在此处查看更多详细信息:

    https://github.com/select2/select2/issues/3503

    在这里解决这个问题:

    https://github.com/select2/select2/commit/c5a54ed70644598529a4071672cca4a22b148806

    【讨论】:

      猜你喜欢
      • 2012-07-21
      • 1970-01-01
      • 2016-06-03
      • 2014-09-19
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多