简体   繁体   English

在AJAX上传期间显示进度条的百分比值

[英]Display percentage value of progress bar during AJAX upload

I can't seem to display my current upload value on my progress bar. 我似乎无法在进度栏上显示当前的上传值。 I know how to do it, if the progress bar has stopped, but it won't work if I want it to update all the time. 如果进度条已停止,我知道该怎么做,但是如果我希望它一直更新,它将无法正常工作。

xhr.upload.onprogress = function(e) {
    if (e.lengthComputable) {
        var percentage = (e.loaded / e.total) * 100;
        $('div.progress-bar').css('width', percentage + '%');
    }
};
<div class="progress progress-striped active hide">
    <div style="width: 0%" class="progress-bar progress-bar-striped active">
        <script>
            $("div.progress-bar").text($("div.progress-bar").width() + "%" );
        </script>
    </div>
</div>

The progress bar itself runs fine. 进度条本身运行正常。 This code just outputs "0%" all the time, as the initial width()-value is 0. I want it to increase along with the progress bar dynamically. 这段代码一直输出“ 0%”,因为初始width()值为0。我希望它随着进度条动态增加。

Thanks in advance! 提前致谢!

You just need to also set the text() of the progress bar too: 您只需要设置进度条的text()

xhr.upload.onprogress = function(e) {
    if (e.lengthComputable) {
        var percentage = (e.loaded / e.total) * 100;
        $('div.progress-bar').css('width', percentage + '%').text(percentage + '%');
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM