简体   繁体   English

从属多选元素

[英]Dependent multi-select Element

I'm trying to create a multi-select element in HTML with 5 options 我正在尝试用5个选项在HTML中创建一个多选元素

<select name="user" id="userName" multiple="multiple">
  <option>ALL</option>
  <option>ALL_2</option>
  <option>ABC</option>
  <option>XYZ</option>
  <option>PQR</option>
</select>

I'd like to have ALL and ALL_2 to be single selects. 我想将ALL和ALL_2设为单选。 In that, what I mean is that if other options are selected along with ALL (or) ALL_2, then ALL (or) ALL_2 are to be deselected). 那就是我的意思是,如果与ALL(或)ALL_2一起选择了其他选项,则ALL(或)ALL_2将被取消选择)。

Ex: Selecting ALL/ALL_2 and then Ctrl + ABC, XYZ should be prevented (or) ALL should be deselected. 例如:选择ALL / ALL_2,然后选择Ctrl + ABC,应避免XYZ(或)取消选择ALL。

Selecting ABC,XYZ,PQR and then selecting ALL / ALL_2 should be prevented (or) ABC,XYZ,PQR should be deselected. 应避免选择ABC,XYZ,PQR,然后选择ALL / ALL_2(或),应取消选择ABC,XYZ,PQR。

I'm looking to do this via MooTools/jQuery by looping through all options to select / deselect, but looking to see if there is a better way to do it since there could be a lot more than 5 options. 我希望通过MooTools / jQuery通过遍历所有选项以选择/取消选择来做到这一点,但希望看看是否有更好的方法可以做到,因为可能有5个以上的选项。

Summary : Options 3-5 are multi-selects but should not be used as multi-selects together with options 1 and 2. Apologies if this is confusing, tried to put my best to communicate it clearly. 简介:选项3-5是多选,但不应与选项1和2一起用作多选。抱歉,如果这很令人困惑,请尽我最大的努力清楚地进行交流。

$('#user_name optgroup option').click(function () {
    // only affects options contained within the same optgroup
    // and doesn't include this
    $(this).siblings().prop('selected', false);
});

Something like this should work for you. 这样的事情应该为您工作。 Just group the option s to an optgroup . 只需将option分组到optgroup Here's a working example . 这是一个有效的例子

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

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