简体   繁体   English

删除在一个下拉列表字段中选择的选项,并且不显示在该行不存在的其他下拉列表中

[英]remove the option selected in one dropdown list field and don't show in other dropdown list present in that row not working- jquery

I have a html table with multiple columns, in two columns i'm displaying the dropdown list. 我有一个包含多列的html表,在两列中我正在显示下拉列表。 When user selects the value from one dropdown list(Select Product1 or Select Product2 dropdown list), i want to remove the option selected in one dropdown and dont show that option in the other dropdown list... 当用户从一个下拉列表(选择Product1或Select Product2下拉列表)中选择值时,我要删除一个下拉列表中选择的选项,而不在另一个下拉列表中显示该选项...

Below sample code works when the class name is same for all the dropdown list available in the table(Select Product1,Select Product2), but in my case the class name for the dropdown list is same for each colum in the table which breaks the below code. 当表中所有可用下拉列表的类名都相同时,下面的示例代码起作用(选择Product1,Select Product2),但在我的情况下,表中每个列的下拉列表的类名都相同,这会破坏以下内容码。

var $combo = $(".combo");
$combo.on("change", function () {
    var select = this,
        selected = $("option:selected", this).text();

    $combo.each(function (_, el) {
        if (el !== select) {
            $("option", el).each(function (_, el) {
                var $el = $(el);
                if ($(el).text() === selected) {
                    $el.remove();
                }
            });
        }
    });
});

Sample Demo : http://plnkr.co/edit/VSdhVfhyIfI0rV6efrZv?p=preview 示例演示: http : //plnkr.co/edit/VSdhVfhyIfI0rV6efrZv?p=preview

In the above demo, when user selects product "laptop" from one dropdown list for one row, the option "laptop" should not be shown in the other dropdown list present in that row... 在上面的演示中,当用户从一个下拉列表中为一行选择产品“笔记本”时,该行中存在的另一个下拉列表中不应显示“笔记本”选项...

Look in same row instead of looping over all the selects in the table. 在同一行中查找,而不是遍历表中的所有选择。 ALso would be simpler to match values 还可以更轻松地匹配值

$combo.on("change", function() {
  var $sel = $(this),
       val = $sel.val();

  $sel.parent().siblings().find('.combo option[value=' + val + ']').remove()

});

Note however that you have a different issue also whereby if user changes one that was previously selected you don't have the ability to re-insert one. 但是请注意,您还有一个不同的问题,即如果用户更改了先前选择的一个,您将无法重新插入一个。

Should consider keeping a clone of all the options stored in a variable so you can look for the missing one during subsequent changes 应该考虑保留所有存储在变量中的选项的克隆,以便您可以在后续更改期间查找缺少的选项

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

相关问题 不要在下拉列表中显示默认选择的选项 - Don't show default selected option in dropdown list 不要在下拉列表中显示所选项目 - Don't show selected item in dropdown list 如何在表中该行的其他可用下拉列表中显示从下拉列表中取消选择的选项-jQuery - how to show the deselected option from the dropdown list in the other available dropdown list of that row in a table - jquery 从下拉列表中删除选择的选项 - Remove selected option from dropdown list 在引导下拉列表菜单中显示选定的选项 - Show selected option in bootstrap dropdown list menu 使用jquery中的选定选项复制/克隆下拉列表 - copy/Clone dropdown list with selected option in jquery jQuery从其他DropDown删除A DropDown中的选定选项,停止工作并挂起 - JQuery to Remove a Selected Option in A DropDown from other DropDowns , stops working and hangs 如何确保如果在一个字段(其他)字段/下拉列表中输入了值,则必须填写/选择 - How to make sure that if value is entered in one field other (accompanying) field/dropdown list must be filled/selected 根据下拉列表中的选定选项显示数据库中的数据 - Show data from database depending on selected option in a dropdown list 从下拉列表中选择选项时,Javascript显示隐藏的div - Javascript to show hidden div when option is selected from dropdown list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM