取消图片上传的上传按钮

时间:2015-03-04 05:53:38

标签: javascript jquery html5 progress-bar

我创建了一个进度条来显示文件上传的进度。现在我正在尝试创建一个"取消上传"进度条旁边的按钮。

进度条的代码JQuery是:

(function () {

                  var bar = $('.bar');
                  var percent = $('.percent');
                  var status = $('#status');
                  var percentVal = '0%';
                  var x = 1;
                  var y = null; // To keep under proper scope


                  $('form').ajaxForm({
                      beforeSend: function () {
                          status.empty();
                          percentVal = '0%';
                          bar.width(percentVal)
                          percent.html(percentVal);
                      },
                      uploadProgress: function (event, position, total, percentComplete) {

                          percentVal = percentComplete + '%';
                          if (percentVal != '100%')
                          {
                            bar.width(percentVal)
                            percent.html(percentVal);
                          }


                          //console.log(percentVal, position, total);
                      },
                      //success: function () {
                      //    var percentVal = '95%';
                      //    bar.width(percentVal)
                      //    percent.html(percentVal);
                      //},
                      complete: function () {

                          percentVal = '100%';
                          bar.width(percentVal)
                          percent.html(percentVal);
                          delay(500);

                          location.reload();
                      }
                  });

              })();

现在为取消按钮输入标签:

 $(document).ready(function () {

        $('#cancel').click(function () {

            location.reload();

        });

    });

Jquery Pulgins:

   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

        <script src="http://malsup.github.com/jquery.form.js"></script>

现在,如果我点击“取消”&#39;按钮图像仍然上传。有人可以帮助我取消&#39;取消&#39;按钮。感谢

3 个答案:

答案 0 :(得分:1)

非常感谢你们的帮助..在花了几分钟寻找解决方案后,我想出了这个解决方案..而且它的工作原理。

(function () {

                  var bar = $('.bar');
                  var percent = $('.percent');
                  var status = $('#status');
                  var percentVal = '0%';
                  var x = 1;
                  var y = null; // To keep under proper scope


                  $('form').ajaxForm({
                      beforeSend: function (xhr) {
                          status.empty();
                          $('#cancel').click(xhr.abort) // for cancel button
                          percentVal = '0%';
                          bar.width(percentVal)
                          percent.html(percentVal);

                      },
                      uploadProgress: function (event, position, total, percentComplete) {

                         percentVal = percentComplete + '%';

                            bar.width(percentVal)
                            percent.html(percentVal);

                            if (percentVal == '100%') {
                                $("#cancel").hide();

                            }

                      },
                      success: function (xhr) {
                          $("#cancel").hide();
                          $('#cancel').click(xhr.abort)

                      },
                      complete: function () {


                          location.reload();
                      }
                  });

              })();

答案 1 :(得分:0)

(function () {

              var bar = $('.bar');
              var percent = $('.percent');
              var status = $('#status');
              var percentVal = '0%';
              var x = 1;
              var y = null; // To keep under proper scope


              $('form').ajaxForm({
                  beforeSend: function () {
                      status.empty();
                      percentVal = '0%';
                      bar.width(percentVal)
                      percent.html(percentVal);
                  },
                  uploadProgress: function (event, position, total, percentComplete) {

                      percentVal = percentComplete + '%';
                      if (percentVal != '100%')
                      {
                        bar.width(percentVal)
                        percent.html(percentVal);
                      }


                      //console.log(percentVal, position, total);
                  },
                  //success: function () {
                  //    var percentVal = '95%';
                  //    bar.width(percentVal)
                  //    percent.html(percentVal);
                  //},
                  complete: function () {

                      percentVal = '100%';
                      bar.width(percentVal)
                      percent.html(percentVal);
                      delay(500);

                      location.reload();
                  }
                  $('#cancel').click(function () {

                    // Abort the data upload if it's running.
                    data.abort();

                    // Overwrite the plugin's default submit function.
                    data.submit = function () { return false; };

                   });
              });

          })();       

});

我认为现在应该可以了。

PS:在Implementing Remove Buttons for jQuery-File-Upload

中找到解决方案

您可能会发现它适合进一步阅读。

答案 2 :(得分:0)

您可以在表单的HTML中创建一个隐藏的输入框

<input id="textHiddenCancel" hidden type="text" value=""/>

修改取消输入按钮的onclick事件,如下所示:

<input id="buttonCancel" type="button" value="Cancel Upload"
       onclick="textHiddenCancel.value = 'cancel';" />

在进度条循环中添加:

if ( textHiddenCancel.value == 'cancel' ) {
    // code to redirect to "you have cancelled" HTML page
    //   or to display a cancelled message on the form
    //   or whatever.
}