简体   繁体   English

更改另一个时如何清除 select (select2)

[英]How to clear a select when changed another (select2)

I have this two selects, where I only have to select one at a time:我有这两个选择,我一次只需要 select 一个:

<div class="col-md-12">
    <div class="form-group">
        <label>Geo Blacklist</label>
        <select name="blacklist[]" multiple="multiple" id="blacklist"
            class="form-control select2"
            data-placeholder="Seleccionar uno o varios países" tabindex="1"
            onchange="$('#whitelist').val([]).change();">
            <option>a</option>
            <option>b</option>
            <option>c</option>
        </select>
    </div>
</div>

<div class="col-md-12">
    <div class="form-group">
        <label>Geo Whitelist</label>
        <select name="whitelist[]" multiple="multiple" id="whitelist"
            class="form-control select2"
            data-placeholder="Seleccionar uno o varios países" tabindex="1"
            onchange="$('#blacklist').val([]).change();">
            <option>x</option>
            <option>y</option>
            <option>z</option>
        </select>
    </div>
</div>

When I select anyone I get:当我 select 得到任何人时:

Uncaught RangeError: Maximum call stack size exceeded at RegExp.exec () at [Symbol.replace] () at String.replace () at Function.camelCase (jquery.js:346:17) at Function.style (jquery.js:6643:22) at jquery.js:6866:12 at jQuery.access (jquery.js:4142:5) at jQuery.fn.init.css (jquery.js:6849:10) at Search.resizeSearch (select2.full.js:2032:18) at DecoratedClass.resizeSearch (select2.full.js:580:32)未捕获的 RangeError:RegExp.exec () 在 [Symbol.replace] () 在 String.replace () 在 Function.camelCase (jquery.js:346:17) 在 Function.style (jquery.js: 6643:22) at jquery.js:6866:12 at jQuery.access (jquery.js:4142:5) at jQuery.fn.init.css (jquery.js:6849:10) at Search.resizeSearch (select2.full .js:2032:18) 在 DecoratedClass.resizeSearch (select2.full.js:580:32)

How can I do this?我怎样才能做到这一点?

Just remove .change() as it trigger the change event on the other select and make infinite loop只需删除.change()因为它触发另一个select上的更改事件并进行无限循环

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-md-12"> <div class="form-group"> <label>Geo Blacklist</label> <select name="blacklist[]" multiple="multiple" id="blacklist" class="form-control select2" data-placeholder="Seleccionar uno o varios países" tabindex="1" onchange="$('#whitelist').val([]);"> <option>a</option> <option>b</option> <option>c</option> </select> </div> </div> <div class="col-md-12"> <div class="form-group"> <label>Geo Whitelist</label> <select name="whitelist[]" multiple="multiple" id="whitelist" class="form-control select2" data-placeholder="Seleccionar uno o varios países" tabindex="1" onchange="$('#blacklist').val([]);"> <option>x</option> <option>y</option> <option>z</option> </select> </div> </div>

This issue is with .这个问题与有关。 When changing a <select> value that's been select2-ised, you have to call .change() to update the UI.更改已被 select2 化的<select>值时,您必须调用.change()来更新 UI。

This then triggers the 2nd select change event, which calls.change etc, creating an infinite loop as shown with Maximum call stack size exceeded .然后这会触发第二个select更改事件,它调用.change 等,创建一个无限循环,如Maximum call stack size exceeded所示。

You can't simply remove the .change() as that would then not update the UI.您不能简单地删除.change() ,因为那样不会更新 UI。

The solution is described in the select2 documentation解决方案在select2 文档中进行了描述

It's common for other components to be listening to the change event, or for custom event handlers to be attached that may have side effects [ such as an infinite loop ].其他组件侦听更改事件或附加可能具有副作用的自定义事件处理程序(例如无限循环)是很常见的。 To limit the scope to only notify Select2 of the change, use the.select2 event namespace:要将 scope 限制为仅通知 Select2 更改,请使用 .select2 事件命名空间:

$('#mySelect2').val('US');
$('#mySelect2').trigger('change.select2'); // Notify only Select2 of changes

In the case of this question that would be to change在这个问题的情况下,这将是改变

onchange="$('#blacklist').val([]).change();">

to

onchange="$('#blacklist').val([]).trigger('change.select2');">

(and the same for whitelist) (白名单也一样)

Here's a snippet (and fiddle ) that demonstrates change.select2 .这是一个演示change.select2的片段(和小提琴)。 Edit and change from change.select2 to change and you get the infinite loop.编辑并从change.select2更改为change ,您将获得无限循环。

 $('.select2').select2(); $('#opt1').on('change', function() { console.log($(this).val()) $("#opt2").val([]).trigger("change.select2"); }); $('#opt2').on('change', function() { $("#opt1").val([]).trigger("change.select2"); });
 <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet" /> <script src="//code.jquery.com/jquery-2.2.4.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script> <select class="select2" id='opt1'> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> </select> <select class="select2" id='opt2'> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM