首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想要修改这段代码,以便只带上class industry = "auto“

我想要修改这段代码,以便只带上class industry = "auto“
EN

Stack Overflow用户
提问于 2019-06-17 05:19:25
回答 1查看 30关注 0票数 0

我有一个jQuery代码,它将所有属性从面板的一侧带到另一侧。此按钮的作用相当于“全部添加”。下面的代码实现了这一点。

到目前为止,我已经做了一些修改,以满足我的目的。我尝试的以下代码仍然可以作为"Add All“

它不断地将所有的值从一个表带到另一个表,而不是那些行业= auto的表。

这是原始的“全部添加”代码。

代码语言:javascript
复制
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;
        });

这就是我到目前为止尝试过的.

代码语言:javascript
复制
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;
        });
EN

回答 1

Stack Overflow用户

发布于 2019-06-18 03:30:14

如果没有更好的环境,就很难知道什么对你不起作用。我唯一能看到的问题就是混淆你.each()中的this

请考虑以下几点:

代码语言:javascript
复制
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没有冲突。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56622712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档