问题
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