Я хочу изменить этот код, чтобы привести только те, которые были класса industry = "auto" - PullRequest
0 голосов
/ 17 июня 2019

У меня есть код jQuery, который переносит все атрибуты с одной стороны панели на другую. Эта кнопка работает как «Добавить все». Следующий код делает это возможным.

Пока что я сделал некоторые изменения, чтобы удовлетворить свои цели. Следующий код, который я пробовал, все еще работает как «Добавить все»

Он продолжает приносить мне все значения из одной таблицы в другую вместо тех, где промышленность = авто.

Это оригинальный код «Добавить все».

this.container.find(".add-all").click(function() {
            var options = that.element.find('option').not(":selected");
            if (that.availableList.children('li:hidden').length > 1) {
                that.availableList.children('li').each(function(i) {
                    if (jQuery(this).is(":visible")) jQuery(options[i-1]).attr('selected', 'selected'); 
                });
            } else {
                options.attr('selected', 'selected');
            }
            that._populateLists(that.element.find('option'));
            return false;
        });

Это то, что я пробовал до сих пор ...

this.container.find(".add-auto").click(function() {
            var options = that.element.find('option').not(":selected");
            var elements = $('li').find("[industry]")
            if (that.availableList.children('li:hidden').length > 1) {
                that.availableList.children('li').each(function(i) {
                    if (jQuery(this).is(":visible") && jQuery(this).find("[industry = 'auto']")) jQuery(options[i-1]).attr('selected', 'selected');
                });
            } else {
                that.availableList.children('li').each(function (i) {
                    if (jQuery(this).find("[industry = 'auto']")) jQuery(options[i-1]).attr('selected','selected');
                });
            }
            that._populateLists(that.element.find('option'));
            return false;
        });

1 Ответ

0 голосов
/ 17 июня 2019

Без лучшего контекста трудно понять, что не работает для вас.Единственное, что я вижу в качестве проблемы, - это путаница this в вашем .each().

. Обратите внимание на следующее:

this.container.find(".add-auto").click(function() {
  var options = that.element.find('option').not(":selected");
  var elements = $('li').find("[industry]")
  if (that.availableList.children('li:hidden').length > 1) {
    that.availableList.children('li').each(function(i, el) {
      if (jQuery(el).is(":visible") && jQuery(el).find("[industry='auto']")){
        jQuery(options[i-1]).attr('selected', 'selected');
      }
    });
  } else {
    that.availableList.children('li').each(function (i, el) {
      if (jQuery(el).find("[industry='auto']")){
        jQuery(options[i-1]).attr('selected','selected');
      }
    });
  }
  that._populateLists(that.element.find('option'));
  return false;
});

Если мы передадим Индекс (i)и Элемент (el), мы можем убедиться, что нет конфликта this.

Надежда, которая помогает.

...