简体   繁体   English

从带有 JS 但不是空项目的 select 列表中删除项目

[英]Remove items from select lists with JS but not empty items

The application uses ColdFusion to read a CSV file and display the column headers for mapping to the column headers our application will accept.该应用程序使用 ColdFusion 读取 CSV 文件并显示列标题以映射到我们的应用程序将接受的列标题。 The number of column headers uploaded will be varied and total number unknown.上传的列标题数量会有所不同,总数未知。 The application acceptable values are displayed in a list.应用程序可接受的值显示在列表中。 Once a value is selected from the list, it cannot be used in any other select list.一旦从列表中选择了一个值,它就不能在任何其他 select 列表中使用。 I have separators with "" values and a typical "Select One" option with a null value.我有带有“”值的分隔符和带有 null 值的典型“选择一个”选项。 When an option in the list is selected, it does remove that option from all the other lists on the page but it also removes the "" options from the active select list which is undesirable because if the user makes a mistake and wants to reset their selection to "", they cannot. When an option in the list is selected, it does remove that option from all the other lists on the page but it also removes the "" options from the active select list which is undesirable because if the user makes a mistake and wants to reset their选择“”,他们不能。 Please help me understand how to keep the JS working as is, but only if the select list items are not "" value.请帮助我了解如何让 JS 保持原样工作,但前提是 select 列表项不是“”值。

// when an option is selected, remove that option from all other select lists

// problem discovered: removes all NULL items (select one and section breaks) from the list too.  need to retain null items 
function checkTheDropdowns() {
  var arr = $('select').find(':selected');

  $('select').find('option').show();

  $.each($('select'), function() {
    var self = this;
    var selectVal = $(this).val();

    console.log("selectVal: " + selectVal);
    console.log("this: " + this);

    //if the selected value is "", do not remove it from the list
    //if (selectVal){

    $.each(arr, function() {
      if (selectVal !== $(this).val()) {
        $(self).find('option[value="' + $(this).val() + '"]').hide()

      } else {
        $(self).find('option[value="' + $(this).val() + '"]').show()
      }
    });

    //}
  })
};

When deciding if an option should be hidden I would use a check for if the value has length before deciding to hiding it.在决定是否应该隐藏一个选项时,我会在决定隐藏它之前检查该值是否有长度。

Since the number 0 is considered falsy in Javascript you can just add a simple if( $(this).val().length ) check.由于数字0在 Javascript 中被认为是虚假的,因此您只需添加一个简单的if( $(this).val().length )检查。

I added my own html to test this and i hope that it is somewhat close to similar to your actual application.我添加了我自己的 html 来测试它,我希望它与您的实际应用程序有点接近。

This would remove options from all other <select> 's except for options with "" value.这将从所有其他<select>中删除选项,但具有""值的选项除外。

 function checkTheDropdowns() { var arr = $('select').find(':selected'); $('select').find('option').show(); $.each($('select'), function() { var self = this; var selectVal = $(this).val(); $.each(arr, function() { if (selectVal === $(this).val()) { $(self).find('option[value="' + $(this).val() + '"]').show(); } else if($(this).val().length) { //<---- This is the actual solution $(self).find('option[value="' + $(this).val() + '"]').hide(); } }); }); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="select-one" onchange="checkTheDropdowns()"> <option selected disabled>Select one</option> <option value=""></option> <option value="a">a</option> <option value="b">b</option> <option value="c">c</option> <option value="d">d</option> <option value="e">e</option> <option value="f">f</option> </select> <select id="select-two" onchange="checkTheDropdowns()"> <option selected disabled>Select one</option> <option value=""></option> <option value="a">a</option> <option value="b">b</option> <option value="c">c</option> <option value="d">d</option> <option value="e">e</option> <option value="f">f</option> </select> <select id="select-three" onchange="checkTheDropdowns()"> <option selected disabled>Select one</option> <option value=""></option> <option value="a">a</option> <option value="b">b</option> <option value="c">c</option> <option value="d">d</option> <option value="e">e</option> <option value="f">f</option> </select>

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

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