按组限制select2选择? [英] Limit select2 selections by group?

查看:184
本文介绍了按组限制select2选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人想出一种方法来限制select2的选择?

Has anyone come up with a way to limit select2's selection by group?

我希望自己从同一组中自动删除任何选择选择组(有效限制为每组选择一个)

I'm hoping to auto-remove any selection from the same group once something from that group is selected (effectively limiting to one selection per group)

我将处理某些事情,如果没有人在我完成之前发布一个好的解决方案,我会在这里发布。

I'll be working on something and will post it here if nobody posts a good solution before I'm finished.

推荐答案

Kevin Brown的回答非常接近我想要的,但我实际上想要自动取消选择任何选定的选项来自同一个optgroup。我得到了以下内容:

Kevin Brown's answer was very close to what I was looking for, but I actually wanted to auto-deselect any selected options from the same optgroup. I got it working with the following:

$("#my-select").on("select2-selecting", function (event) {
    $(event.object.element).parent("optgroup").find(":selected").attr('selected', false);
    $("#my-select").trigger('change');
});

注意:我使用的是Select2 3.5.2

NOTE: I'm using Select2 3.5.2

这篇关于按组限制select2选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆