【问题标题】:Make image hide correctly after filtering using jquery使用jquery过滤后使图像正确隐藏
【发布时间】:2013-10-30 03:25:55
【问题描述】:

我正在使用以下代码过滤表中的数据并在执行此操作时显示 ID 为 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/

【问题讨论】:

标签: javascript jquery html image filter


【解决方案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();           
            }  
       });
      }           
    });
});    

http://jsfiddle.net/m6hLR/10/

【讨论】:

    【解决方案2】:

    如果没有带有一些 HTML 的 jsfiddle 来理解你的代码结构是相当困难的,但是试试这个吧。

    http://jsfiddle.net/MRa8q/3/

    $('#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();
    });
    

    【讨论】:

    • 我添加了一个带有一些细节的 jsFiddle
    【解决方案3】:

    您尚未在图像标签上添加 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">
    

    JsFiddle : http://jsfiddle.net/subins2000/m6hLR/7/

    【讨论】:

    • 糟糕...刚刚修好了
    • 我还是有这个问题
    • 不,它只是隐藏它。它应该在查找数据时显示,然后在停止时隐藏...让我将表格加长以向您展示我的意思
    • 看:尝试过滤并寻找 1 jsfiddle.net/m6hLR/9 过滤不正确
    猜你喜欢
    • 2014-05-01
    • 2019-03-03
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多