[英]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.