最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I disable, hide, or remove selected option in other selects with the same options? - Stack Overflow

programmeradmin0浏览0评论

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.

No jQuery, only plain JavaScript please.

If possible I would like the first option to always display in all <select> elements:

<option class="select-items" selected>Sélectionnez..</option>

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>

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;
     pteur.textContent = ` ${total}/${custSelec.length -1}`;

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.

No jQuery, only plain JavaScript please.

If possible I would like the first option to always display in all <select> elements:

<option class="select-items" selected>Sélectionnez..</option>

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>

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;
     pteur.textContent = ` ${total}/${custSelec.length -1}`;
Share Improve this question edited May 14, 2019 at 13:26 benvc 15.2k4 gold badges38 silver badges57 bronze badges asked May 13, 2019 at 15:49 Florent VicenteFlorent Vicente 1051 silver badge6 bronze badges 3
  • You can keep the first option in another select and show/hide the current select on the click event listener of that option – A Rogue Otaku Commented May 13, 2019 at 15:53
  • What would you prefer to do? 1.) Disable the other options? 2.) Hide the other options? 3.) Remove the other options? – scunliffe Commented May 13, 2019 at 16:59
  • 1 With UI/UX in mind (apparently a must now), i would say that disable would be best. But ultimately any help is fine ^^ – Florent Vicente Commented May 14, 2019 at 7:21
Add a ment  | 

3 Answers 3

Reset to default 4

In your change event listener, you can get the current set of selected values from all <select> elements in the group, and then loop through each element's options to both disable the options currently selected elsewhere in the group as well as re-enable any options that were previously selected but have since been changed. You can avoid disabling the first "label" option in each of your selects by checking the value before disabling / enabling options.

You could use this same approach to hide or remove options keeping in mind that there are some browser patibility issues when trying to hide <option> elements and that you would need some additional code to store the plete list of options if you were going to remove and restore them.

const selects = document.querySelectorAll('.select-group');
selects.forEach((elem) => {
  elem.addEventListener('change', (event) => {
    const values = Array.from(selects).map((select) => select.value);
    for (const select of selects) {
      select.querySelectorAll('option').forEach((option) => {
        const value = option.value;
        if (value &&  value !== select.value && values.includes(value)) {
          option.disabled = true;
        } else {
          option.disabled = false;
        }
      });
    }
  });
});
<select class="select-group">
  <option value="">Select...</option> 
  <option value="first">First Value</option> 
  <option value="second">Second Value</option>
  <option value="third">Third Value</option>
</select>
<select class="select-group">
  <option value="">Select...</option> 
  <option value="first">First Value</option> 
  <option value="second">Second Value</option>
  <option value="third">Third Value</option>
</select>
<select class="select-group">
  <option value="">Select...</option> 
  <option value="first">First Value</option> 
  <option value="second">Second Value</option>
  <option value="third">Third Value</option>
</select>

Thanks a lot for your help! I added a small 'if' to fix my bug and it works perfectly (until the next bug ^^):

if(value !== ""){
option.disabled = true;
}

Or I could just :

if (value && value !== select.value && values.includes(value) && value !== "") {
option.disabled = true;            
} 

Another difficulty when you begin : learn to write simple code ^^z

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论