开发者

How can I use `appendChild` to add unlimited File Upload fields?

开发者 https://www.devze.com 2023-03-20 16:48 出处:网络
Im a little new to JavaScript. I am going to have a file upload field. I want to add an onclick to the file upload field to add additional file upload fields.

Im a little new to JavaScript.

I am going to have a file upload field. I want to add an onclick to the file upload field to add additional file upload fields.

So I'm thinking something like:

i=0
function addfile() { 
document.getElementById("form_name").appendChild(<input type=\"file\" name=\"file1\"" +     "i++" + " />)}

...then...

<input type="file" onclick="addfile();" />

(I know this syntax is probably terrible/missing things, I'm just trying to layou开发者_Python百科t the concept I think I am supposed to be using.)

Can you get this working?


This encapsulates the i variable.

(function() { 
    var i = 0;
    window.addfile = function() { 
        var input = document.createElement('input');
        input.type = 'file';
        input.name = 'file' + i++;

        document.getElementById('form_name').appendChild(input);
    }
})();

jsFiddle.

0

精彩评论

暂无评论...
验证码 换一张
取 消