如何禁用、隐藏或删除具有相同选项的其他选择中的选定选项? [英] How do I disable, hide, or remove selected option in other selects with the same options?

查看:38
本文介绍了如何禁用、隐藏或删除具有相同选项的其他选择中的选定选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个包含 4 个 元素中的一个 元素中选择相同的选项.

I'm writing a form with 4 <select> elements. They all have the same options and I would like to disable, hide, or remove the selected option from one <select> in the other <select> elements with the same options in order to prevent the user to select the same option in multiple <select> elements.

请不要使用 jQuery,请使用纯 JavaScript.

No jQuery, only plain JavaScript please.

如果可能,我希望第一个选项始终显示在所有 的 HTML:

Here is the HTML for one <select>:

<select class="custom-select  mb-3" id="name_typage_0">
  <option class="select-items" selected>Sélectionnez..</option>
  <option class="select-items" value="designation">Désignation</option>
  <option class="select-items" value="email">Email</option>
  <option class="select-items" value="ville">Ville</option>
  <option class="select-items" value="secteur_activite">Secteur d'activité</option>
</select>

这是我的 JavaScript 的一部分:

Here is part of my JavaScript:

const custSelec = document.querySelectorAll('.custom-select');
custSelec.forEach(function(item){
   item.addEventListener('change', function(){
      if(item.options[item.selectedIndex].text == 'Sélectionnez..'){
         count = -1;
      }else{
         count = 1;
      total += count;
     compteur.textContent = ` ${total}/${custSelec.length -1}`;

推荐答案

在您的 change 事件侦听器中,您可以从所有

发送“验证码”获取 | 15天全站免登陆