我使用此代码过滤我的<select>
菜单:
jQuery.fn.filterByText = function(textbox) {
return this.each(function() {
var select = this;
var options = [];
$(select).find('option').not('option:first-child').each(function() {
options.push({value: $(this).val(), text: $(this).text(), rel: $(this).attr('rel')});
});
$(select).data('options', options);
$(textbox).bind('change keyup', function() {
var options = $(select).empty().data('options');
var search = $.trim($(this).val());
var regex = new RegExp(search,"gi");
$.each(options, function(i) {
var option = options[i];
if(option.text.match(regex) !== null) {
$(select).append(
$('<option>').text(option.text).val(option.value)
);
}
});
});
});
};
如您所见,我使用.not('option:first-child')
排除第一个<option>
标签。
这段代码有两个问题,它工作正常,但我需要在过滤中包含该属性,并且我想在清除文本框时rel
重新添加。option:first-child
我已将其添加rel: $(this).attr('rel')
到我的options
数组中,但我不确定如何将其添加回过滤结果中?
我可以创建var original = [];
并将所有原始值推入其中吗?