我正在使用以下代码过滤表格中的数据,并在执行此操作时显示标识为loader
的图像。
我的问题是它没有隐藏,我不知道我做错了什么。
我尝试在隐藏之前添加警报,它确实显示警报,但在显示图像时它无法正常工作。我究竟做错了什么? inputFilter
是过滤器输入的名称,data_fm_op
是表的名称。
的JavaScript
$('#inputFilter').change(function () {
$('#loader').show();
var that = this;
$('tbody tr').each(function () {
if ($(this).text().indexOf($(that).val()) == -1) {
$('#data_fm_op').animate({
marginTop: 0
}, 50, function () {
$(this).find('tbody tr').eq(i).hide();
});
} else {
$('#data_fm_op').animate({
marginTop: 0
}, 50, function () {
$(this).find('tbody tr').eq(i).show();
});
}
});
$('#loader').hide();
});
以下是jsFiddle: http://jsfiddle.net/m6hLR/
答案 0 :(得分:1)
你可以等待animate回调,如果当前索引等于tr的长度,那么隐藏加载器
$('#loader').hide();
$('#inputFilter').change(function() {
$('#loader').show();
var that = this;
var length=$('tbody tr').length;
$('tbody tr').each(function(i,n) {
if ($(this).text().indexOf($(that).val()) == -1) {
$('#data_fm_op').animate({
marginTop: 0
}, 50, function() {
$(this).hide();
if(i==length-1){//if it is the last element hide the loader
$('#loader').hide();
}
});
} else {
$('#data_fm_op').animate({
marginTop: 0
}, 50, function() {
$(this).show();
if(i==length-1){//if it is the last element hide the loader
$('#loader').hide();
}
});
}
});
});
答案 1 :(得分:0)
如果没有jsfiddle和一些HTML来理解你的代码结构,那就很难了。但是试试这个。
$('#inputFilter').change(function() {
$('#loader').show();
var that = this;
$('tbody tr').each(function() {
if ($(this).text().indexOf($(that).val()) == -1) {
$('#data_fm_op').animate({
marginTop: 0
}, 50, function() {
$(this).hide();
});
} else {
$('#data_fm_op').animate({
marginTop: 0
}, 50, function() {
$(this).show();
});
}
});
$('#loader').hide();
});
答案 2 :(得分:0)
您尚未在图片代码上添加 id 属性。变化:
<img src="https://www.vocalware.com/images/ajax_loader.gif" height="19" width="19">
到:
<img src="https://www.vocalware.com/images/ajax_loader.gif" id="loader" height="19" width="19">