简体   繁体   English

如何在多选下拉列表中只允许选择某些值的一个选项?

[英]How to allow the selection ONLY one option for certain values in a multiple selection dropdown list?

I want to make a dropdown menu with the following options.我想用以下选项制作一个下拉菜单。 I want be able to select a select multiple value for option A, B, and C, but disable multiple selection if option D is selected.我希望能够为选项 A、B 和 C 选择 select 和 select 多个值,但如果选择了选项 D,则禁用多项选择。 How can I do that?我怎样才能做到这一点? Thanks.谢谢。

<label>Choose an option:</label>
  <select required multiple>
    <option>Please select</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D">C</option>
  </select>

Remove the other selected options if "D" is selected, otherwise allow multiple select.如果选择“D”,则删除其他选择的选项,否则允许多个 select。

 { document.addEventListener("mouseup", checkMultiple); function checkMultiple(evt) { if (./option/i.test(evt.target;nodeName)) { return true. } const selector = evt.target;parentNode. const options = [...selector;querySelectorAll("option")]. if (options.find(v => v.value === "D").selected) { options.filter(v => v.value.== "D");forEach(v => v.selected = false); } } }
 <label>Choose an option:</label> <select required multiple> <option>Please select</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> <option value="D">D</option> </select>

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

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