javascript - HTML + JS - 添加新文件按钮时文件名在文本中重置

标签 javascript html file reset filechooser

我有一个添加按钮,就像这样,可以添加更多 <input type="file"> : enter image description here

它确实有效,当我点击按钮时,它会调用一个方法来添加一个新的文件选择器。

HTML:

<div class="row" id="attachments_row">
          <div class="coffee-span-4">
            <label class="label label-1">Attachments</label>
          </div>
          <div class="coffee-span-6" id="attachments">
            <div id="attachment_inner">
                <input id="file_button" type="file" name="fileUpload0" size="50" /> 
                <sf:errors path="filepath" cssClass="rfqerror"></sf:errors>
            </div>
            <input type="button" value="+" style="float: left; margin-top: 10px; margin-bottom: 10px; padding: 5px;" onclick="makeNewAttachment();"/>
          </div>

          <div class="coffee-span-2" id="file-upload-button">
          </div>

          <br>
</div>

JS:

function makeNewAttachment() {
    var ai = document.getElementById("attachment_inner").innerHTML;
    var index = document.getElementById("attachment_inner").children.length;
    var ai_new = ai + "<input id='file_button' type='file' name='fileUpload" + index + "' size='50' />";
    document.getElementById("attachment_inner").innerHTML = ai_new;
}

但是当我选择一个文件时,文件名重置回 No file chosen .

选择一个文件:

enter image description here

点击添加,重置?!?!?!?

enter image description here

有人知道为什么吗? 我把每个名字都改成了不同的,我以为是这样,但不是!

最佳答案

问题是您复制的是 div #attachment_inner 中的 HTML 文本,而不是作为 div 对象子对象的文档对象。表示文件选择器 input 的文本没有表示最近选择的文件名的属性。该信息位于 input 对象中。虽然该对象是从 HTML 文本构建的,但该文本不会被重写以反射(reflect)新信息,例如所选文件的名称。

解决方案是使用cloneNode()对要复制的对象进行深度复制。

在下面的代码片段中,我克隆了包含文件选择器的 div,以便您也拥有 sf:errors 标记的副本以及您可能希望与按钮一起拥有的任何其他内容.此外,容器 div 使布局更容易。为避免多个 div 具有相同的 id,我将 id="attachment_inner" 更改为 class="attachment_inner"

function makeNewAttachment() {
    var attachments = document.getElementById('attachments'),
        choosers = attachments.getElementsByClassName('attachment_inner'),
        numChoosers = choosers.length,
        newChooser = choosers[numChoosers - 1].cloneNode(true),
        input = newChooser.getElementsByTagName('input')[0];
    attachments.insertBefore(newChooser, document.getElementById('plusButton'));
    input.name = input.name.replace(/\d+$/, '' + numChoosers);
}
<div class="row" id="attachments_row">
          <div class="coffee-span-4">
            <label class="label label-1">Attachments</label>
          </div>
          <div class="coffee-span-6" id="attachments">
            <div class="attachment_inner">
                <input type="file" name="fileUpload0" size="50" /> 
                <sf:errors path="filepath" cssClass="rfqerror"></sf:errors>
            </div>
            <input id="plusButton" type="button" value="+" style="float: left; margin-top: 10px; margin-bottom: 10px; padding: 5px;" onclick="makeNewAttachment();"/>
          </div>

          <div class="coffee-span-2" id="file-upload-button">
          </div>

          <br>
</div>

关于javascript - HTML + JS - 添加新文件按钮时文件名在文本中重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298057/

相关文章:

html - 如何使用html和css创建倾斜的div的下边缘?

html - 省略号在 firefox 中运行不佳,但在 chrome 中运行良好

android - 如何在 Android Studio 中获取原始资源的文件路径?

javascript - 重构三元运算符

javascript - 将 JavaScript 代码放入 <a> 的不同方法之间有什么区别?

javascript - 文件上传/预览,无法更改图像尺寸

javascript - 在 javascript 中从一个字节中获取两个半字节的最佳方法?

html - 页脚重叠动态生成的表格

mysql - Web 应用程序的文件存储 : Filesystem vs DB vs NoSQL engines

python - 使用 python 从文本文件中删除两个重复项(原始和重复项)