duanjianshen4871 2019-04-20 13:04 采纳率: 100%
浏览 346

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

I'm currently trying to create a button that allows me to group options based on an attribute. Since I'm no jquery wiz, I took the existing and working button "Add-all" and started modifying it in order to add only the options with a certain attribute. The following code is that intent...

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;
});
},

I'm currently using the has.class in order to look for the attribute that i want. In this case , auto. But it's not working. I'm trying to fix the code shown on top, in order to call the attribute "auto". the label = "'+ option.attr('class')+'" is the code that generates the lable = "auto" in html.

_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;
},

Below there is an image that shows the user interface. When I inspect the AUTO: option , i have the following code...

<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

I want my Add auto button to be able to move all industry = "auto" options to the left.

  • 写回答

1条回答 默认 最新

  • duancaiyi7567 2019-04-20 15:09
    关注

    I am just going to base my answer on your Title

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

    that code is going to return all elements with an attribute of "data-attribute"

    just change document if you want to find on specific places like inside of div#Header ... also change data-attribute to what you want like industry or title or even data-selected-value

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条