【问题标题】:How to Add/Remove select option from 1 drop down on selection of other dropdown option如何在选择其他下拉选项时从 1 个下拉列表中添加/删除选择选项
【发布时间】:2016-05-20 14:43:18
【问题描述】:

假设我有 2 个下拉菜单。我的要求是,在第一个下拉列表中选择一个选项时,该选项应该从其他下拉列表中删除,并且在从第一个下拉列表中取消选择该选项时,该选项应该在其他下拉列表中可供选择。

【问题讨论】:

  • 给所有链接的选项一个相似的ID,比如'option_1_left'和'option_1_right'。在您选择的 onchange 事件中,使用下拉列表的 .selectedIndex 来获取正确的选项。 (或者在选项上使用 onclick 处理程序并使用 event.target)。一旦您选择了选项,获取其 ID,将“左”替换为“右”。然后你有你应该从另一个下拉列表中删除的选项的 ID。

标签: javascript html salesforce option dropdown


【解决方案1】:

我编写了以下 JavaScript 来解决上述问题,只需在此处为您的下拉菜单添加一个“重要性”之类的通用类并使用此代码:

            var j$importance = j$(".importance");

            j$importance.on("change", function () {

                var select = this,
                    selected = j$("option:selected", this).text();

                var currentUnselectedValues = [];

                j$('option', this).each(function() { 

                    currentUnselectedValues.push( j$(this).text());
                });

                j$importance.each(function (_, el) {

                    for(i=0; i<currentUnselectedValues.length; i++){

                       var exist = false;

                       j$('option', el).each(function(_, elm) { 

                           if(currentUnselectedValues[i] === j$(elm).text()){

                               exist = true;
                           }
                       });

                       if(exist === false){

                          j$(el).append('<option value="' + currentUnselectedValues[i] + '">' + currentUnselectedValues[i] + '</option>');
                       }
                    }

                    if (el !== select) {
                        j$("option", el).each(function (_, el) {
                            var j$el = j$(el);
                            if (j$el.text() === selected && j$el.text() != '--None--') {
                                j$el.remove();
                            }
                        });
                    }
                });

             });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2017-05-19
    相关资源
    最近更新 更多