添加到innerHTML而不破坏表单内容
我有一个通过基于多文件上传器(swfupload)的ajax生成的表单。它会在每个文件上传完成后向给定的 dom 元素添加更多表单元素。这给我带来了一个问题。如果我选择上传 5 个文件,第一个文件将生成一个表单,我将开始在其中输入数据,但是,当第二个文件上传完成时,它会清除表单元素中先前输入的数据,并附加第二个表单元素。
我认为这是因为我使用:
document.getElementById('test').innerHTML = document.getElementById('test').innerHTML + newformelements;
我认为执行上述操作不会在表单中保留任何输入的数据,而只是 HTML 本身。
那么,如何附加到此元素而不破坏已放入表单字段中的内容呢?可能的子项数量是根据多重上传者动态变化的。
I have a form that is generated via ajax based of a multi-file uploader (swfupload). It adds more form elements to a given dom element upon the completion of each file uploaded. This thus gives me a problem. If i selected 5 files to upload, the first file will generate a form which I will start entering data in, however, when the 2nd file is completed uploading, it clears the previous entered data in the form elements and also appends the 2nd form elements.
I think this is because im using:
document.getElementById('test').innerHTML = document.getElementById('test').innerHTML + newformelements;
I think doing the above doesn't keep any entered data in the forms, just the HTML itself.
So, how can I append to this element without destroying what has been put into form fields already? The number of possible children is dynamic based of the multi-uploader.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你愿意使用 jquery 吗?如果是的话你可以轻松地做这样的事情
Are you open to use jquery? If yes then You can easily do something like this
使用 DOM 操作,在本例中:
Node.appendChild
[文档]。innerHTML
将总是销毁并重新创建元素。Use DOM manipulation, in this case:
Node.appendChild
[docs].innerHTML
will always destroy and recreate the elements.创建一个新元素,设置其innerHTML,然后将其附加到表单子元素的末尾。
Create a new element, set its innerHTML, than append it to the end of the forms' children.