[英]Add additional file upload fields
I am using this code to allow the user to create additional upload fields.我正在使用此代码允许用户创建其他上传字段。 The fields are being created and the user can select a file but it is never added to the input field.
正在创建字段,用户可以 select 一个文件,但它永远不会添加到输入字段中。 The only one that works is the first one.
唯一有效的是第一个。 In firebug, I noticed there is no event listener for the additonal fields that are created.
在 firebug 中,我注意到创建的附加字段没有事件侦听器。
<script>
$(document).ready(function () {
var next = 1;
$(".add-more").click(function (e) {
e.preventDefault();
var addto = "#field" + next;
next = next + 1;
var newIn = '<div id="field' + next + '" class="custom-file mt-3 mb-2"><label class="custom-file-label">NewFile ' + next + '</label><input class="custom-file-input" name="field' + next + '" type="file"><input type = "text" class="form-control mt-1 mb-1" name="field' + next + '" placeholder="Text" /></div>';
var newInput = $(newIn);
$(addto).after(newInput);
$("#field" + next).attr('data-source', $(addto).attr('data-source'));
$("#count").val(next);
});
});
This is the HTML for the input that already exists and for the new put that is generated when a user clicks on the button to add new files:这是 HTML 用于已经存在的输入和当用户单击按钮添加新文件时生成的新放置:
<div id="fields">
<div id="field1" class="custom-file mt-1 mb-3">
<input class="custom-file-input" id="field-1" name="field-1" type="file">
<label class="custom-file-label">New File 1</label>
<input type="text" class="form-control mt-1 mb-1" placeholder="New File">
</div>
<div id="field2" class="custom-file mt-3 mb-2">
<label class="custom-file-label">New File 2</label>
<input class="custom-file-input" name="field2" type="file">
<input type="text" class="form-control mt-1 mb-1" name="field2" placeholder="Please provide a brief description of the uploaded file">
</div>
<button id="b1" class="btn btn-success add-more mt-4" type="button">Add more files</button>
</div>
Just a note;只是一个注释; I am using Bootstrap 4.
我正在使用引导程序 4。
Does anybody know the cause of this?有人知道这是什么原因吗?
EDIT: I have just added this code and when I try to upload using the dynamically created inputs, the alert is 'undefined'编辑:我刚刚添加了这段代码,当我尝试使用动态创建的输入上传时,警报是“未定义”
$("#fields").delegate("input[type=file]", "change", function () {
alert($(this).attr("id"));
});
I have created a fiddle using the code kindly provided by Bhushan and it appears to be working just fine.我使用 Bhushan 提供的代码创建了一个小提琴,它似乎工作得很好。 I think I may have a conflict within my application.
我想我的申请中可能有冲突。
http://jsfiddle.net/kogcyc/x1hphsvb/ http://jsfiddle.net/kogcyc/x1hphsvb/
Thanks谢谢
you are attaching change event handler for only fields input and not to the new file input added.您只为字段输入而不是添加的新文件输入附加更改事件处理程序。
please repalce请更换
$("#fields").delegate("input[type=file]", "change", function () {
alert($(this).attr("id"));
});
with -- here we are attaching change event handler using .on
and selector will look for all file input having id starts with fields with -- 这里我们使用
.on
附加更改事件处理程序,并且选择器将查找所有具有 id 以字段开头的文件输入
$(document).on("change","input[id^=field][type=file]", function () {
alert($(this).attr("id"));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.