简体   繁体   English

使用Javascript动态生成文件上传框

[英]Using Javascript to dynamically generate file upload boxes

I'm trying to write an html/javascript set (to be interpreted in PHP) that allows the user to select a file from a, and then as soon as that file is selected it displays another.我正在尝试编写一个 html/javascript 集(用 PHP 解释),它允许用户从 a 中 select 一个文件,然后一旦选择该文件,它就会显示另一个。

function displayFileInput() {
var counter = document.getElementById("counter").value;
var n = (parseInt(counter)+1).toString();
var element = document.getElementById("container");
var string = "<br /><input type = \"file\" name = \"file"+n+"\" onchange =\"javascript:displayFileInput()\"  />Default:<input type = \"radio\" name = \"main_picture\" value = \""+n+"\"  />";
element.innerHTML = element.innerHTML + string;
document.getElementById("counter").value=n;
return false;}

The HTML code I'm using is this:我使用的 HTML 代码是这样的:

<form enctype=\"multipart/form-data\" method = \"post\" action=\"newPost.php\" id = \"rental_form\">
<input type = \"text\" name = \"rent_name\" size = \"30\" value = \"Insert your post title here\"/><br />
<textarea rows= \"20\" cols = \"100\" name = \"rent_posttext\">
Insert your post text here!
</textarea><br />
<div id = \"container\">
<input type = \"file\" name = \"file1\" onchange = \"javascript:displayFileInput()\" />
Default:<input type = \"radio\" name = \"main_picture\" value = \"1\"  />
</div>
<input type = \"submit\" />
<input type = \"text\" id = \"counter\" value = \"1\" style = \"display:noe;\">

Everything seems to work at first, but the problem is this: as soon as I select a file, it correctly displays another file box, but gets rid of the information in the previous file.起初一切似乎都正常,但问题是:一旦我 select 一个文件,它正确显示另一个文件框,但摆脱了前一个文件中的信息。 For instance, in Google Chrome, next to the "choose file" input button it will say "No file chosen."例如,在 Google Chrome 中,“选择文件”输入按钮旁边会显示“未选择文件”。

When I submit the form to my PHP script, it recognizes the files, but registers a "4" error for each one which means that no file was uploaded.当我将表单提交到我的 PHP 脚本时,它会识别文件,但会为每个文件注册一个“4”错误,这意味着没有上传任何文件。 Somehow, it's getting rid of all the file information when it dynamically generates a new file input.不知何故,当它动态生成一个新的文件输入时,它会摆脱所有的文件信息。 Help!帮助!

Your issue is being caused by the part that goes like:您的问题是由以下部分引起的:

element.innerHTML = element.innerHTML + string;

This is completely replacing the content of your element, and wiping out any previous user selection(s) in the process.这完全替换了您元素的内容,并在此过程中消除了任何先前的用户选择。 You will get a better result if you use appendChild instead of innerHTML =...;如果你使用appendChild而不是innerHTML =...;你会得到更好的结果, but that will require re-working your code a bit. ,但这将需要重新处理您的代码。

Something like this should work:像这样的东西应该工作:

function displayFileInput() {
    var counter = document.getElementById("counter").value;
    var n = (parseInt(counter)+1).toString();
    var element = document.getElementById("container");
    var newInput = document.createElement("div");
    var string = "<input type = \"file\" name = \"file"+n+"\" onchange =\"javascript:displayFileInput()\"  />Default:<input type = \"radio\" name = \"main_picture\" value = \""+n+"\"  />";
    newInput.innerHTML = string;
    element.appendChild(newInput);
    document.getElementById("counter").value=n;
    return false;
}

Example: http://jsfiddle.net/dsKFr/示例: http://jsfiddle.net/dsKFr/

Also, you don't need to escape all the quotes in your HTML markup.此外,您不需要转义 HTML 标记中的所有引号。 Just the JavaScript version (and even there you could get around it by alternating single quotes with your double quotes).只是 JavaScript 版本(即使在那里,您也可以通过将单引号与双引号交替来绕过它)。

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

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