(function($, window, document, undefined) {
$.fn.quicksearch = function (target, opt) {
var timeout, cache, rowcache, jq_results, val = '', e = this, options = $.extend({
delay: 100,
selector: null,
stripeRows: null,
loader: null,
noResults: '',
bind: 'keyup',
onBefore: function() {
return;
},
onAfter: function() {
return;
},
show: function() {
this.style.display = "";
},
hide: function() {
this.style.display = "none";
},
prepareQuery: function (val) {
return val.toLowerCase().split(' ');
},
testQuery: function (query, txt, _row) {
for (var i = 0; i < query.length; i += 1) {
if (txt.indexOf(query[i]) === -1) {
return false;
}
}
return true;
}
}, opt);
this.go = function() {
var i = 0,
noresults = true,
query = options.prepareQuery(val),
val_empty = (val.replace(' ', '').length === 0);
for (var i = 0, len = rowcache.length; i < len; i++) {
if (val_empty || options.testQuery(query, cache[i], rowcache[i])) {
options.show.apply(rowcache[i]);
noresults = false;
} else {
options.hide.apply(rowcache[i]);
}
}
if (noresults) {
this.results(false);
} else {
this.results(true);
this.stripe();
}
this.loader(false);
options.onAfter();
return this;
};
this.stripe = function() {
if (typeof options.stripeRows === "object" && options.stripeRows !== null)
{
var joined = options.stripeRows.join(' ');
var stripeRows_length = options.stripeRows.length;
jq_results.not(':hidden').each(function (i) {
$(this).removeClass(joined).addClass(options.stripeRows[i % stripeRows_length]);
});
}
return this;
};
this.strip_html = function (input) {
var output = input.replace(new RegExp('<[^<]+\>', 'g'), "");
output = $.trim(output.toLowerCase());
return output;
};
this.results = function (bool) {
if (typeof options.noResults === "string" && options.noResults !== "") {
if (bool) {
$(options.noResults).hide();
} else {
$(options.noResults).show();
}
}
return this;
};
this.loader = function (bool) {
if (typeof options.loader === "string" && options.loader !== "") {
(bool) ? $(options.loader).show() : $(options.loader).hide();
}
return this;
};
this.cache = function() {
jq_results = $(target);
if (typeof options.noResults === "string" && options.noResults !== "") {
jq_results = jq_results.not(options.noResults);
}
var t = (typeof options.selector === "string") ? jq_results.find(options.selector) : $(target).not(options.noResults);
cache = t.map(function() {
return e.strip_html(this.innerHTML);
});
rowcache = jq_results.map(function() {
return this;
});
return this.go();
};
this.trigger = function() {
this.loader(true);
options.onBefore();
window.clearTimeout(timeout);
timeout = window.setTimeout(function() {
e.go();
}, options.delay);
return this;
};
this.cache();
this.results(true);
this.stripe();
this.loader(false);
return this.each(function() {
$(this).bind(options.bind, function() {
val = $(this).val();
e.trigger();
});
});
};
}(jQuery, this, document));
我揣摩在哪?我怎么能做出一个分流/数字和字母之间添加空间的代码。导致一些人输入例如“ip1500”,脚本不能匹配输入和“ip 1500”之类的元素。我的问题在于我是初学者。
我试图尝试,但我不能得到它的工作。我也试过this
我发现这点,我认为它可以在这里完成,其中的一切都得到了一个“”(空格)分裂:
prepareQuery: function (val) {
return val.toLowerCase().split(' ');
},
将是非常好的,如果有人能帮助我。
非常感谢你,这工作得很好,但现在这拆分每一个字母和数字。例如:我输入“pixma ip1500”并且脚本甚至匹配一个像这样的元素:“pixam pim 015”会导致每个单独的数字,并且来自输入的字母位于li元素“pixam pim 015”中。换句话说,li元素包含来自输入的每一个数字和字母。对不起,我的英语不好! – Kuba
@xtramaster任何想法:)? – Kuba
Finaly我修复了每个单一数字或字母的分割问题。我改变了你最后一行“return tempArray.join(”“);”到“return tempArray.join(”“)。split('');”。所以我只是添加这个“.split('');” – Kuba