简体   繁体   English

如何在文件上传和追加期间禁用上传按钮

[英]How to disable upload button during file upload and append

By fluke I realise an issue which would cause a big problem with my application. 通过侥幸,我意识到一个问题会导致我的应用程序出现大问题。

I have my application here where it will upload videos. 我的应用程序在这里上传视频。 Application 应用

The problem I have is that if you upload a video and then click on the Add Question button to append another file input in a new row, the Upload button in the new row is not disabled, this means that you can upload multiple files which I do not want as it will stop the first file uploading to stop uploading and potentially system ould crash if users try to upload large multiple files. 我遇到的问题是,如果你上传一个视频,然后点击Add Question按钮将另一个文件输入Add Question到新行,新行中的Upload按钮不会被禁用,这意味着你可以上传多个文件我不要因为它会阻止第一个文件上传以停止上传,如果用户尝试上传大型多个文件,系统可能会崩溃。 My question is that if I click on the Add Question button , if a file is uploading, then the upload button in the appended row(s) should be disabled until upload is finished? 我的问题是,如果我点击Add Question按钮,如果文件正在上传,那么应该禁用所附行中的上传按钮,直到上传结束?

To use app follow steps below: 要使用应用,请按以下步骤操作

  1. Click on Add Question twice to append 2 new table rows. 单击Add Question两次以附加2个新表行。 In each table row you will see a file input. 在每个表行中,您将看到文件输入。

  2. In file input in row 1 select a video (I select a decent size video so that file is uploading while you complete other steps) and click on Upload to upload video. 在第1行的文件输入中选择一个视频(我选择一个体面的视频,以便在您完成其他步骤时上传文件),然后单击Upload以上传视频。 You will now see that the Upload buttons are disabled while video is uploading 您现在将看到在Upload视频时禁用Upload按钮

  3. Now while video is uploading click on Add Question button again to append another row. 现在,在上传视频时,再次单击“ Add Question按钮以附加另一行。 Now you will see that the Upload button in the newly appended row is not disabled while the file is uploading. 现在,您将看到在上Upload文件时未禁用新附加行中的“上Upload按钮。 I want this button and other upload buttons which are appended to be disabled if a file is uploading. 我希望在上传文件时禁用此按钮和其他附加的上传按钮。

Below is Relevant code where it appends the table rows from the Add Question button: 下面是Add Question按钮附加表行的相关代码:

function GetFormVideoCount(){ 
  return $('.videouploadform').length + 1;
}


var qnum = 1;
var qremain = 5;

function insertQuestion(form) {   

    if (qnum > 5) {
    return;
}


    var $tbody = $('#qandatbl_onthefly > tbody');
    var $tr = $("<tr class='optionAndAnswer' align='center'>");
    var $video = $("<td width='17%' class='video'></td>"); 

              $('.num_questions').each( function() {

    var $fileVideo = $("<form action='videoupload.php' method='post' enctype='multipart/form-data' target='upload_target_video' onsubmit='return videoClickHandler(this);' class='videouploadform' >" + 
    "<p class='videof1_upload_form'><label>" + 
    "Video File: <input name='fileVideo' type='file' class='fileVideo' /></label><br/><br/><label class='videolbl'>" +  
    "<input type='submit' name='submitVideoBtn' class='sbtnvideo' value='Upload' /></label>" + 
    "<p class='videof1_upload_process' align='center'>Loading...<br/><img src='Images/loader.gif' /></p>" +
    "<input type='hidden' class='numvideo' name='numvideo' value='" + GetFormVideoCount() + "' />" + </p>" +
    "<iframe class='upload_target_video' name='upload_target_video' src='/' style='width:0px;height:0px;border:0px;solid;#fff;'></iframe></form>"); 

    $video.append($fileVideo);


    $tr.append($video);  
    $tbody.append($tr); 


}

Below is relevant code where when it starts the file's uploading process and where it disables the upload buttons in rows which are already appended: 下面是相关代码,其中它开始文件的上传过程,并禁用已经附加的行中的上传按钮:

   function startVideoUpload(videouploadform){

             sourceVideoForm = videouploadform;

            $(".sbtnvideo").attr("disabled", "disabled");


    }); 

          return true;
    }

Below is relevant code for function when uploading has finished: 以下是上传完成后功能的相关代码:

function stopVideoUpload(success, videoID, videofilename){


      $(".sbtnvideo").removeAttr("disabled");


      return true;   
}

Below is video upload click handler: 以下是视频上传点击处理程序:

   function videoClickHandler(videouploadform){ 
      if(videoValidation(videouploadform)){ 
          window.lastUploadVideoIndex = $('.videouploadform').index(videouploadform);
          return startVideoUpload(videouploadform); 
      } 
      return false;
  }

UPDATE: 更新:

function startVideoUpload(videouploadform){


               $(".sbtnvideo").click(function(event){
               $(".sbtnvideo").attr("disabled", "disabled");
    event.preventDefault();
    });

       return true;   
    }

    function stopVideoUpload(success, videoID, videofilename){


                $(".sbtnvideo").click(function(event){
                $(".sbtnvideo").removeAttr("disabled");
    return true;
});


          return true;   
    }

You can prevent the default behavior of the button then reenable it later. 您可以阻止该按钮的默认行为,然后再重新启用它。

$(".sbtnvideo").click(function(event){
    event.preventDefault();
});

$(".sbtnvideo").click(function(event){
    return true;
});

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

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