How do I appendChild a file upload field with i++?

牧云@^-^@ 提交于 2019-12-12 05:44:19

问题


I im creating a javascript file using addElemend and childAppend to add a new

Ive written the code here http://jsfiddle.net/faYMH/18/

But for some reason it isnt working. If I replace the file upload field within the innerhtml with a simple it does work!

Can anyone spot the problem / put in some input how to accomplish what i want?

Thanks!

Jonah


回答1:


There's a few things wrong with that.

JSFiddle problems

First of all, you still have the framework settings set to "onLoad" and "Mootools". You'll want it to be one of the "no wrap" options and "No-Library (pure JS)". Secondly, you're putting the script in a script tag in the HTML pane. There's a JavaScript pane specifically for JavaScript.

JavaScript problems

You have some inline HTML in your JavaScript:

newDiv.innerHTML = "<input type="file" name="file1 + i++" />";

You're using double quotes (") for your JavaScript string as well as inside the HTML. Try using single quotes for the JavaScript string delimiters, like this:

newDiv.innerHTML = '<input type="file" name="file1' + (i++) + ' />';

The HTML inside the string is also not valid. It will try to generate HTML like this:

<input type="file" name="file10 />

There's no closing quote. Fix that:

newDiv.innerHTML = '<input type="file" name="file1' + (i++) + '" />';

You might also want to remove the stray 1, though it doesn't break the script.

Result

Here it is, after these changes and a few more.



来源:https://stackoverflow.com/questions/6688320/how-do-i-appendchild-a-file-upload-field-with-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!