简体   繁体   English

如何从应用了 Select2 的 SELECT 中删除选项?

[英]How to remove a option from a SELECT with Select2 applied?

So, I have this HTML code:所以,我有这个 HTML 代码:

<input type="checkbox" id="orders_lives_in_ccs" name="orders[lives_in_ccs]" class="lives_in_ccs">
<select id="orders_shipping_from" name="orders[shipping_from]" required="required" class="shipping_from toSelect2">
    <option value="" selected="selected">-- SELECCIONAR --</option>
    <option value="MRW">MRW - COBRO EN DESTINO</option>
    <option value="DOMESA">DOMESA - COBRO EN DESTINO</option>
    <option value="ZOOM">GRUPO ZOOM - COBRO EN DESTINO</option>
</select>

I need to remove the option MRW when checkbox .lives_in_ccs is checked so I made this code:当复选框.lives_in_ccs被选中时,我需要删除选项MRW所以我做了这个代码:

$(function () {
    $('.toSelect2').select2();

    var detachedMember;
    $('.lives_in_ccs').click(function () {
        if (this.checked) {
            detachedMember = $('.shipping_from option[value="MRW"]').detach();
        } else {
            $('.shipping_from option[value=""]').after(detachedMember);
        }

        $(".secure_shipping").toggle(this.checked);
    });
});

This code works but I'm having a problem and didn't found the way to fixit.此代码有效,但我遇到了问题并且没有找到修复它的方法。 Having this jsFiddle do this tests:这个jsFiddle 做这个测试:

  • Leave the SELECT with the default value which is --SELECCIONAR-- and mark the check in the left side, that will works and will remove the option MRW from the main SELECT, if you unmark the check the value will appear again on the SELECT将 SELECT 保留为默认值--SELECCIONAR--并在左侧标记检查,这将起作用并将从主 SELECT 中删除选项MRW ,如果取消标记该检查,该值将再次出现在 SELECT
  • Choose MRW (the first choice) on the SELECT and mark the check in the left side, that will remove the option MRW from the main SELECT, but will leave the option as SELECTED which is wrong since the server side isn't expecting it.在 SELECT 上选择MRW (第一选择)并标记左侧的检查,这将从主 SELECT 中删除选项MRW ,但会将选项保留为 SELECTED,这是错误的,因为服务器端不期望它。

So, how do I remove the option even if it's selected on the SELECT element?那么,即使在 SELECT 元素上选择了该选项,我该如何删除它? Any help?有什么帮助吗?

Here is the updated fiddle for testing: http://jsfiddle.net/fgaqgpd7/2/这是用于测试的更新小提琴: http : //jsfiddle.net/fgaqgpd7/2/

This will reset the value of the select2 input:这将重置 select2 输入的值:

$('.toSelect2').select2('val','');

You can apply it you click event handler.您可以在单击事件处理程序时应用它。 Basically if MRW is selected, then reset the select2 value.基本上,如果选择了 MRW,则重置 select2 值。

$(function () {
    $('.toSelect2').select2();
    var detachedMember;
    $('.lives_in_ccs').click(function () {
        if (this.checked) {
            if ($('.toSelect2').select2('val') == 'MRW') {
                $('.toSelect2').select2('val','');
            }
            detachedMember = $('.shipping_from option[value="MRW"]').detach();
        } else {
            $('.shipping_from option[value=""]').after(detachedMember);
        }

        $(".secure_shipping").toggle(this.checked);
    });
});

try this :尝试这个 :

if($(".lives_in_ccs").is(':checked')){ 
    $("#orders_shipping_from option[value='MRW']").remove();
}

Hope this helps :)希望这可以帮助 :)

For me in version Select2 4.0.13:对于我在 Select2 4.0.13 版本中:

$('#my-form').on('change', '.checkbox-item', updateDropdown);

HTML object: HTML 对象:

<input type="checkbox" name="checkbox_1" id="checkbox_1" value="1" class="form-control checkbox-item" data-text="Label" />

Event function:事件功能:

    function updateDropdown(e) {
    if (e.target.checked) {
        if ($('#dropdown').select2('val') == $(this).val()) {
            $('#dropdown').select2('val', '');
        } else {
            var newOption = new Option($(this).attr('data-text'), $(this).val(), false, false);
            $('#dropdown').append(newOption).trigger('change');
        }
    } else {
        $('#dropdown option[value="' + $(this).val() + '"]').detach();
    }
}

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

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