我有这样的代码,使用下拉列表对表进行生命过滤。
$(document).ready(function () {
$('.filter').change(function () {
var values = [];
$('.filter').each(function () {
var colIdx = $(this).data('col');
$(this).find('option:selected').each(function () {
if ($(this).val() != "") values.push( {
text: $(this).text(),
colId : colIdx
});
});
});
filter('table > tbody > tr', values);
});
function filter(selector, values) {console.log(values);
$(selector).each(function () {
var sel = $(this);
var tokens = sel.text().trim().split('\n');
var toknesObj = [], i;
for(i=0;i<tokens.length;i++){
toknesObj[i] = {
text:tokens[i].trim(),
found:false
};
}
var show = false;
//console.log(toknesObj);
$.each(values, function (i, val) {
if (toknesObj[val.colId].text.search(new RegExp("\\b"+val.text+"\\b")) >= 0) {
toknesObj[val.colId].found = true;
}
});
console.log(toknesObj);
var count = 0;
$.each(toknesObj, function (i, val) {
if (val.found){
count+=1;
}
});
show = (count === values.length);
show ? sel.show() : sel.hide();
});
}
});我需要一个修改来使用相同的代码,但是使用一个搜索按钮来运行过滤器,而不是使用生命过滤器。也许问题在于.change(function (),但我不知道如何实现使用搜索按钮而不是生命过滤器的方法。
谢谢
发布于 2022-06-11 09:36:36
通过将.change前面的选择器替换为按钮的引用,并将.change替换为.click可能适用于您的情况。
$(document).ready(function () {
$('.button-selector').click(function () {
...
});
...
});
https://stackoverflow.com/questions/72583084
复制相似问题