在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推送到
列表中,我在Chosen.prototype.choice_build
中添加了这一行'data-user-id': item.user_id
,最终它看起来像:
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);
};
就是这样!您可以使用此方法推送任意数量的自定义属性。
chosen
中找不到任何默认的属性/方法来完成这项工作。你必须使用 jQuery 自定义代码来实现它。 - Mohammad