如何根据Jquery中的属性对元素进行分组?

问题描述 投票:1回答:1

我目前正在尝试创建一个按钮,允许我根据属性对选项进行分组。由于我不是jquery wiz,我使用现有的工作按钮“Add-all”并开始修改它以便仅添加具有特定属性的选项。以下代码是意图......

this.container.find(".remove-all").click(function() {
  that._populateLists(that.element.find('option').removeAttr('selected'));
  return false;
});

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");
  if (that.availableList.children('li:hidden').length > 1) {
    that.availableList.children('li').each(function(i) {
      if (jQuery(this).is(":visible") && jQuery(this).has.class("auto")) jQuery(options[i - 1]).attr('selected', 'selected');
    });
  } else {
    that.availableList.children('li').each(function(i) {
      if (jQuery(this).has.class("auto")) jQuery(options[i - 1]).attr('selected', 'selected');
    });
  }
  that._populateLists(that.element.find('option'));
  return false;
});
},

我目前正在使用has.class来查找我想要的属性。在这种情况下,自动。但它不起作用。我正在尝试修复顶部显示的代码,以便调用属性“auto”。 label = "'+ option.attr('class')+'"是在html中生成lable =“auto”的代码。

_getOptionNode: function(option) {
  option = jQuery(option);
  var node = jQuery('<li class="ui-state-default ui-element "  title="' + option.text() + '" data-selected-value="' + option.val() + '" label = "' + option.attr('class') + '"  ><span class="ui-icon"/>' + option.text() + '<a href="#" class="action"><span class="ui-corner-all ui-icon"/></a></li>').hide();
  node.data('optionLink', option);
  return node;
},

下面是一个显示用户界面的图像。当我检查AUTO:选项时,我有以下代码......

<li class="ui-state-default ui-element  ui-draggable ui-draggable-handle" title="AUTO: Car Dealers - New Cars (CARD_NEW)" data-selected-value="82" industry="auto'" style=""><span class="ui-helper-hidden"></span>AUTO: Car Dealers - New Cars (CARD_NEW)<a href="#" class="action"><span class="ui-corner-all ui-icon ui-icon-plus"></span></a></li>

User Interface

我希望我的添加自动按钮能够将所有行业=“自动”选项移动到左侧。

php jquery html web multi-select
1个回答
0
投票

我只想根据你的标题给出答案

var elements = $(document).find("[data-attribute]")

该代码将返回具有"data-attribute"属性的所有元素

只是改变document如果你想找到像div#Header内部的特定地方...也改变data-attribute你想要的像industrytitle甚至data-selected-value

© www.soinside.com 2019 - 2024. All rights reserved.