繁体   English   中英

Bootstrap进度条超过100%

[英]Bootstrap Progress Bar Going Past 100%

我正在制作带有进度条的codeingiter文件上传脚本。 在我的Java脚本函数上,当我单击图像进行上传时,它会触发进度条以设定的时间间隔启动。

但是由于某种原因,当达到100%时,它仍会继续超过100%。

问题:进度条达到100%时,如何在脚本上强制停止进度条?

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="progress progress-striped active">
<div class="progress-bar" role="progressbar" aria-valuenow="0"  aria-valuemin="0" aria-valuemax="100" style="width: 0%">
<span class="sr-only">0% Complete</span>
</div>
</div>
<div class="image"></div>
</div>
</div>

<script type="text/javascript">
$('#button-upload').on('click', function() {
    $('#form-upload').remove();

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" value="" /></form>');

    $('#form-upload input[name=\'file\']').trigger('click');

    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }

    timer = setInterval(function() {

        if ($('#form-upload input[name=\'file\']').val() != '') {

            clearInterval(timer);

            setTimeout(function(){

            $('.progress .progress-bar').each(function() {

            var me = $(this);
            var perc = me.attr("data-percentage");

            var current_perc = 0;

            var progress = setInterval(function() {
            if (current_perc>=perc) {
                clearInterval(progress);
            } else {
                current_perc +=1;
                me.css('width', (current_perc)+'%');
            }

            me.text((current_perc) +' %');

            }, 50);

            });

            },300); 

            $.ajax({
                url: 'admin/common/filemanager/upload/?directory=<?php echo $directory; ?>',
                type: 'post',       
                dataType: 'json',
                data: new FormData($('#form-upload')[0]),
                cache: false,
                contentType: false,
                processData: false,     
                beforeSend: function() {
                    $('#button-upload i').replaceWith('<i class="fa fa-circle-o-notch fa-spin"></i>');
                    $('#button-upload').prop('disabled', true);
                },
                complete: function() {
                    $('#button-upload i').replaceWith('<i class="fa fa-upload"></i>');
                    $('#button-upload').prop('disabled', false);
                },
                success: function(json) {
                    if (json['error']) {
                        alert(json['error']);
                    }

                    if (json['success']) {
                        //alert(json['success']);

                        //$('#button-refresh').trigger('click');
                    }
                },          
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }); 
        }
    }, 500);
});
</script>

我发现了我的问题

在这里需要有data-percentage =“ 100”

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="progress progress-striped active">
<div class="progress-bar" role="progressbar" data-percentage="100"  aria-valuenow="0"  aria-valuemin="0" aria-valuemax="100" style="width: 0%">
<span class="sr-only">0% Complete</span>
</div>
</div>
<div class="image"></div>
</div>
</div>

脚本

<script type="text/javascript">
$('#button-upload').on('click', function() {
    $('#form-upload').remove();

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" value="" /></form>');

    $('#form-upload input[name=\'file\']').trigger('click');

    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }

    timer = setInterval(function() {

        if ($('#form-upload input[name=\'file\']').val() != '') {

            clearInterval(timer);

            $.ajax({
                url: 'admin/common/filemanager/upload/?directory=<?php echo $directory; ?>',
                type: 'post',       
                dataType: 'json',
                data: new FormData($('#form-upload')[0]),
                cache: false,
                contentType: false,
                processData: false,     
                beforeSend: function() {
                    $('#button-upload i').replaceWith('<i class="fa fa-circle-o-notch fa-spin"></i>');
                    $('#button-upload').prop('disabled', true);

                },
                complete: function() {
                    $('#button-upload i').replaceWith('<i class="fa fa-upload"></i>');
                    $('#button-upload').prop('disabled', false);
                },
                success: function(json) {
                    if (json['error']) {
                        alert(json['error']);
                    }

                    if (json['success']) {

                    $('.progress .progress-bar').each(function() {

                    var me = $(this);
                    var perc = me.attr("data-percentage");

                    var current_perc = 0;

                    var progress = setInterval(function() {

                    if (current_perc>=perc) {

                        clearInterval(progress);


                        $('#button-refresh').trigger('click');


                    } else {
                        current_perc +=1;
                        me.css('width', (current_perc)+'%');


                    }

                    me.text((current_perc) +' %');

                    }, 50);

                    });

                    }
                },          
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }); 
        }
    }, 500);
});
</script>

暂无
暂无

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

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