如何从多个文件上传中清除javascript中的一个输入类型文件

时间:2015-08-13 06:58:30

标签: javascript c# file-upload

我有2个文件上传字段,每个字段都有'清除'按钮。 当我上传文件并点击其中一个''清除'按钮然后全部 文件上传很清楚。但我需要清除一个文件。

请看我的代码

<input type="file" id="control1"/>
<button id="clear1">Clear</button>

<input type="file" id="control2"/>
<button id="clear2">Clear</button>


var control = $("#control1");

$("#clear1").on("click", function () {
    control1.replaceWith( control1 = control1.clone( true ) );
});


var control2 = $("#control2");

$("#clear2").on("click", function () {
    control2.replaceWith( control2 = control2.clone( true ) );
});

1 个答案:

答案 0 :(得分:1)

试试这个:

<script type="text/javascript">
        function clear2() {
          var fileUpload = document.getElementById("<%=control2.ClientID %>");
          var id = fileUpload.id;
          var name = fileUpload.name;

          //Create a new FileUpload element.
          var newFileUpload = document.createElement("INPUT");
          newFileUpload.type = "FILE";

          //Append it next to the original FileUpload.
          fileUpload.parentNode.insertBefore(newFileUpload, fileUpload.nextSibling);

          //Remove the original FileUpload.
          fileUpload.parentNode.removeChild(fileUpload);

          //Set the Id and Name to the new FileUpload.
          newFileUpload.id = id;
          newFileUpload.name = name;
          return false;
      }
      function clear1() {
          var fileUpload = document.getElementById("<%=control1.ClientID %>");
          var id = fileUpload.id;
          var name = fileUpload.name;

          //Create a new FileUpload element.
          var newFileUpload = document.createElement("INPUT");
          newFileUpload.type = "FILE";

          //Append it next to the original FileUpload.
          fileUpload.parentNode.insertBefore(newFileUpload, fileUpload.nextSibling);

          //Remove the original FileUpload.
          fileUpload.parentNode.removeChild(fileUpload);

          //Set the Id and Name to the new FileUpload.
          newFileUpload.id = id;
          newFileUpload.name = name;
          return false;
      }
    </script>

    <input type="file" id="control1"/>
    <input id="Button1" type="button" value="clickme" onclick="clear1();" />

    <input type="file" id="control2"/>
     <input id="clickMe" type="button" value="clickme" onclick="clear2();" />