【问题标题】:update a JQuery progress bar added at runtime更新运行时添加的 JQuery 进度条
【发布时间】:2010-11-02 16:13:52
【问题描述】:

我在更新 jquery 进度条时遇到了一些问题。在页面加载期间,此进度条不在文档中,我只是在用户单击按钮时添加它,如下所示:

$(this).parent().append('<div class="progressbar"></div>');
$(this).parent().children('div.progressbar').show();
$(this).parent().children('div.progressbar').progressbar({value: 20});

然后,使用超时,我正在尝试更新它

function updateProgressBar() {
    $('.progressbar').each(function() {
        myNewValue = getNewValue();
        $(this).progressbar('value', 50);
    });
    setTimeout('updateProgressBar()', 5000);
}
setTimeout('updateProgressBar()', 5000);

调试控制台抱怨说:“未捕获:无法在初始化之前调用进度条上的方法:试图调用方法‘值’” 在这里谷歌搜索我发现问题可能与进度条的初始化有关页面加载后

有人可以帮我吗?

提前致谢

-- 编辑--

感谢 Bryan,我正在尝试您的解决方案,但我不适合我

现在我有了这段代码

function startProgress() {

    $(this).parent().append('<div class="progressbar"></div>');
    $(this).siblings('.progressbar').show();
    $(this).siblings('.progressbar').progressbar({value: 0});

    function updateProgress() {
        $('.progressbar').each(function() {
            myNewValue = getNewValue($(this).parent().parent().attr('id'));
            $(this).progressbar('value', myNewValue);
        });
        setTimeout('updateProgress', 5000);
    }
    setTimeout('updateProgress', 5000);
}

控制台说没有定义 updateProgress

-- 编辑--

非常感谢!!!

现在我有了一个非常确定的版本......

这是我当前的代码

if($(this).siblings('.progressbar').size() == 0) {
        $(this).parent().append('<div class="progressbar"/>');
        $(this).siblings('.progressbar').progressbar({value: 0});
}
$(this).siblings('.progressbar').show();

function updateProgress() {
    $('.progressbar').each(function() {
        myParams = 'service=' + $(this).parent().parent().attr('id') + '&content=' + $(this).parent().attr('id')
        myUrl = '/datacast/content_progress/?' + myParams;
        theValue = $(this).progressbar('value');
        $.get(myUrl, {}, function(aReply) {
            myData = aReply.split(' ');
            myItemId =  myData[0];
            myValue = parseInt(myData[1]);
            try {
                $(".item[id = " + myItemId + "]").children(".progressbar").progressbar('value', myValue);
            }
            catch(myError) {
                //alert(myError);
            }
        })
    });
    setTimeout(updateProgress, 5000);
}
setTimeout(updateProgress, 5000);

如您所见,如果已经有一个进度条,我已经添加了一个控件,因为我多次通过该代码。 进度条每次都会更新,但控制台抱怨说“TypeError:无法调用未定义的方法'apply'”,所以我不得不添加带有空catch主体的try块来删除错误。该页面有效,但如果您知道为什么会出现该错误,它可能会很有趣

【问题讨论】:

  • 你用的是什么插件?
  • 我正在使用 jquery-1.4.2.js 和 jquery UI Progressbar 1.7.2
  • 在下面查看我的新答案。

标签: javascript jquery progress-bar


【解决方案1】:

遇到同样的问题

显然你必须第一次使用progressbar({value:30})这个格式

如果您第一次使用progressbar(value,30),则会收到此异常。

【讨论】:

  • 对象符号实际上看起来像 progressbar({value:30})
  • 我只是在 Firefox 中遇到了这个问题,而且这个更改有效。
【解决方案2】:

好吧,我不敢相信我错过了。问题是您将字符串传递给 setTimeout 函数。这将导致它在全局范围内查找函数的名称,而事实并非如此。

更改这两个调用:

setTimeout('updateProgress', 5000);

setTimeout(updateProgress, 5000);

【讨论】:

  • 谢谢!现在它可以工作了,即使在我找不到的地方出现错误,请查看我的上次编辑
【解决方案3】:

确保您在更新方法中使用与初始化方法中完全相同的选择器。

在提供的代码中,您正在执行类似$(this).parent().children().find('.progressbar') 的操作,然后在更新中您只是在执行$('.progressbar')。第二次调用可能会返回第一次调用没有的项目,并且这些项目不会初始化进度条。

这段代码对我来说很好用:

$(function(){
    $('body').append('<div class="progress"></div>');

    var val = 10;
    $('.progress').progressbar({value:val});

    function updateProgress() {
        val += 10;
        $('.progress').progressbar('value', val);
        if(val < 100)
            setTimeout(updateProgress, 1000);
    }

    setTimeout(updateProgress, 1000);
});

另外,请记住,您实际上并不需要调用 each(),因为 jquery 方法应该自动应用于与该选择器匹配的所有元素。

例子:

$('.red').each(function(){ $(this).css({color:'red'}); });

是多余的,同样可以通过以下方式实现:

$('.red').css({color:'red'});

哦,这是一个免费赠品:

$(this).parent().children().find('.progressbar')

可简写为:$(this).siblings('.progressbar')

【讨论】:

  • 我发布了一个新答案,检查一下。
  • 这越来越多毛了。你能发布一些应该使用的示例 HTML 吗?
猜你喜欢
  • 2019-05-18
  • 2021-05-29
  • 2015-07-04
  • 2019-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多