admin管理员组文章数量:1353235
I need to add multiple
attribute to input type file
but I don't know how to do it.
Below My code:
var input = document.createElement("input");
input.type = "file";
input.id = "files" + af;
input.name = "imgs[]";
input.className = "upload";
But I need the input to have multiple
atribute.
I need to add multiple
attribute to input type file
but I don't know how to do it.
Below My code:
var input = document.createElement("input");
input.type = "file";
input.id = "files" + af;
input.name = "imgs[]";
input.className = "upload";
But I need the input to have multiple
atribute.
-
1
Use this:
input.setAttribute('multiple','');
– behzad besharati Commented Mar 13, 2017 at 12:30 - That's what I was looking for, thank you @behzadbesharati – Carlos Vinicius F. Gracioli Commented Mar 13, 2017 at 12:42
3 Answers
Reset to default 8Just use setAttribute()
function like below example :
As ou can see you can create input that accepts only image :
var af = 1;
var input = document.createElement("input");
input.setAttribute("type","file");
input.setAttribute("id" ,"files" + af);
input.setAttribute("name","imgs[]");
input.setAttribute("multiple","");
input.setAttribute("accept","image/*");
input.className = "upload";
document.body.appendChild(input);
console.log(input);
input.setAttribute('multiple','');
Solved the problem.
None of the answers fully explain this, though they are somewhat correct. The problem is that the 'multiple' attribute must be set AFTER adding the element to the DOM. I'm not sure why it works this way, but I could not get this to work until I changed the order of execution. Here is how it should look:
const fileElement = document.createElement("input");
fileElement.type = 'file';
document.body.appendChild(fileElement);
fileElement.setAttribute('multiple', '');
In my case, I have made this element hidden (with additional code), so I just attached it to the body. You'll want to attach it to a parent element that makes more sense if you are displaying this element.
本文标签: javascriptHow to add quotmultiplequot attribute to dynamic generated Input FileStack Overflow
版权声明:本文标题:javascript - How to add "multiple" attribute to dynamic generated Input File? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743899799a2558466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论