简体   繁体   English

如何清除文件输入元素

[英]How to clear file input element

I have an application here where the user can add a row and it displays a file input.这里有一个应用程序,用户可以在其中添加一行并显示文件输入。

I have 2 problems when trying to clear a file input:尝试清除文件输入时遇到 2 个问题:

Problem 1: If I add 2 rows in the application, if I fill both file inputs and I click on 'Click File' button in one of the rows, then instead of clearing the file within the row, it is clearing the file in all the rows which is incorrect, it should clear the file within the row I wanted to clear file on.问题 1:如果我在应用程序中添加 2 行,如果我填写两个文件输入并单击其中一行中的“单击文件”按钮,那么它不会清除行内的文件,而是清除所有文件不正确的行,它应该清除我想清除文件的行中的文件。

Problem 2: After I try uploading a file by clicking on "Upload" button, after uploading is succcessful or not, if I select another file and then try to clear it by clicking on 'Clear File', then it doesn't clear the file at all.问题2:在我尝试通过点击“上传”按钮上传文件后,上传成功与否后,如果我select另一个文件然后尝试通过点击“清除文件”来清除它,那么它不会清除根本没有文件。

How can both of these problems be fixed:如何解决这两个问题:

<script type="text/javascript">
 var sourceImageForm; 

function insertQuestion(form) {   

    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
    var $image = $("<td class='image'></td>"); 


    var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startImageUpload(this);' class='imageuploadform' >" + 
    "<p class='imagef1_upload_process' align='center'>Loading...<br/><img src='Images/loader.gif' /><br/></p><p class='imagef1_upload_form' align='center'><br/><label>" + 
    "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label class='imagelbl'>" + 
    "(jpg, jpeg, pjpeg, gif, png, tif)</label><br/><br/><label>" + 
    "<input type='submit' name='submitBtn' class='sbtn' value='Upload' /></label>" + 
    "<label><input type='button' name='imageClear' class='imageClear' value='Clear File'/></label>" +
    "</p> <iframe class='upload_target' name='upload_target' src='#' style='wclassth:0;height:0;border:0px;solclass #fff;'></iframe></form>");      

    $image.append($fileImage);

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

      $(".imageClear").click(function(event){
  event.preventDefault();
  $(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");
});

}

function startImageUpload(imageuploadform){
  $(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
  $(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
  sourceImageForm = imageuploadform;
      return true;
}

function stopImageUpload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      $(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
      $(sourceImageForm).find('.imagef1_upload_form').html(result + '<label>Image File: <input name="fileImage" type="file"/></label><br/><label>(jpg, jpeg, pjpeg, gif, png, tif)</label><br/><br/><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /></label><label><input type="button" name="imageClear" class="imageClear" value="Clear File"/></label>');
      $(sourceImageForm).find('.imagef1_upload_form').css('visibility','visible');     
      return true;   
}

</script>

The below code will clear all file inputs present in the page as you are pointing to the class当您指向 class 时,以下代码将清除页面中存在的所有文件输入

 $(".imageClear").click(function(event){
  event.preventDefault();
  $(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage'   
      />");
  });

replace with用。。。来代替

$(".imageClear").click(function(event){
  event.preventDefault();
  $(this).closest(".fileImage").replaceWith("<input type='file' class='fileImage'    
    name='fileImage'/>");
  });

I hope it will work...我希望它能奏效...

Problem 1 is caused by this line:问题 1 是由这一行引起的:

$(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");

That means that when you click in any "Clear file" button all the elements that have the class "fileImage" are replaced, not only the one that belongs to the same form as the button.这意味着当您单击任何“清除文件”按钮时,所有具有 class“fileImage”的元素都会被替换,而不仅仅是与按钮属于相同形式的元素。 Replacing it with the following should work:将其替换为以下内容应该有效:

 $(this).parents("form:first").find(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");

Since what I'm doing with the jquery selector is, first selecting the form the button clicked belongs to, and then finding the "fileImage" element inside that form.由于我对 jquery 选择器所做的是,首先选择单击的按钮所属的表单,然后在该表单中找到“fileImage”元素。

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

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