【问题标题】:Select2 - Infinite loop with trigger('change')Select2 - 带触发器的无限循环('change')
【发布时间】:2013-03-23 21:12:11
【问题描述】:

这是我的情况:

$('select').select2();

$('select').on('change', function () {
    // calling a function
    myFunction();
});

function myFunction() {
    // changes my select values
    // so I need to update the select for seing the news values
    $('select').trigger('change');

    // hehe I fire the change event so myFunction is called again and again
}

我能做些什么来避免这种行为?问候...

【问题讨论】:

    标签: jquery-select2


    【解决方案1】:

    这是 Select2 中的错误。我对以下代码有同样的问题:

    var FID = $(location).attr('href').split("/")[5];
    $('#facility').children().each(function () {
        if ($(this).val().trim() == FID.trim()) {
            $(this).attr('selected', 'selected').trigger('change');
        }
    });
    

    以下内容并不理想,但确实可以解决问题。请注意,您需要重新定义您的 Select2 选项(如图所示)。

    var FID = $(location).attr('href').split("/")[5];
    $('#facility').children().each(function () {
        if ($(this).val().trim() == FID.trim()) {
            $(this).attr('selected', 'selected');
            $('#facility').select2({
                placeholder: "",
                minimumResultsForSearch: -1
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 2021-07-02
      相关资源
      最近更新 更多