javascript - jQuery-chosen - 将所选选项的自定义选项属性推送到列表

标签 javascript jquery jquery-chosen

我有一个如下所示的选项列表

<select data-placeholder="Choose users" style="width:350px;" multiple class="chosen-select" tabindex="8">
    <option data-user-id="1">User1</option>
    <option data-user-id="3">User2</option>
    <option data-user-id="6">User3</option>
    <option data-user-id="14">User4</option>
</select>

我可以使用 chosen plugin 选择多个选项。每个选定的值都输入到新的 <li> 中元素如下:

<li class="search-choice"><span>User</span><a class="search-choice-close" data-option-array-index="2"></a></li>

有没有办法将 data-user-id 从选项推送到上面 <li> .

我可以在不破解所选插件的情况下做到这一点吗?

Below you will find my solution by changing a little bit chosen plugin

最佳答案

里面有SelectParser.prototype.add_option我使用 jquery 获取了自定义属性,然后将其作为选项推送。

SelectParser.prototype.add_option看起来像:

SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
  if (option.nodeName.toUpperCase() === "OPTION") {
    if (option.text !== "") {
      if (group_position != null) {
        this.parsed[group_position].children += 1;
      }
      this.parsed.push({
        user_id: $(option).attr('data-user-id'),
        array_index: this.parsed.length,
        options_index: this.options_index,
        value: option.value,
        text: option.text,
        html: option.innerHTML,
        title: option.title ? option.title : void 0,
        selected: option.selected,
        disabled: group_disabled === true ? group_disabled : option.disabled,
        group_array_index: group_position,
        group_label: group_position != null ? this.parsed[group_position].label : null,
        classes: option.className,
        style: option.style.cssText
      });
    } else {
      this.parsed.push({
        array_index: this.parsed.length,
        options_index: this.options_index,
        empty: true
      });
    }
    return this.options_index += 1;
  }
};

正如你所注意到的,我只放了这个 user_id: $(option).attr('data-user-id'),代码行。

然后将 data-user-id 推送到 <li>列表我添加此行 'data-user-id': item.user_idChosen.prototype.choice_build所以最后它看起来像:

  Chosen.prototype.choice_build = function(item) {
      var choice, close_link,
        _this = this;
      choice = $('<li />', {
        "class": "search-choice"
      }).html("<span>" + (this.choice_label(item)) + "</span>");
      if (item.disabled) {
        choice.addClass('search-choice-disabled');
      } else {
        close_link = $('<a />', {
          "class": 'search-choice-close',
          'data-option-array-index': item.array_index,
          'data-user-id': item.user_id
        });
        close_link.bind('click.chosen', function(evt) {
          return _this.choice_destroy_link_click(evt);
        });
        choice.append(close_link);
      }
      return this.search_container.before(choice);
    };

就是这样!您可以通过这种方式推送任意数量的自定义属性。

关于javascript - jQuery-chosen - 将所选选项的自定义选项属性推送到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38226295/

相关文章:

javascript - 选择 JS 按关键字搜索

html - 选择 : chosen-drop got cut off behind table 的响应表

javascript - 从 JS 数组中选择一个值并跳过它,直到显示所有值

javascript - 即使放入错误的可放置对象,如何禁用可拖动对象

jquery - "Chaining"到可拖动调用以启用触摸功能

javascript - 将动态属性添加到对象末尾

javascript - 在 bootstrap 弹出窗口中选择的 jquery 不起作用

javascript - "cannot read property"和无法获取属性之间的区别?

Javascript : open popup in the center of the screen (Chrome)

jquery - 如何绑定(bind)到元素数据属性