将表单从字符串附加到 DOM
我的 Mootools 函数获取一个 JSON 对象,它是字符串格式的 html 表单
var req = new Request.JSON({
method: 'post',
url: 'index.php',
onSuccess: function(r) {
if (typeof(r)!='undefined' && r!=null)
{
$('my_div').set('html',r.form);
}
});
有没有办法将字符串响应转换为 HTML 元素,以便我以后可以这样做:
document.myForm.submit();
我知道我可以解析每个表单元素并创建 DOM 元素一个接一个,但是有没有一种更简单的方法将表单从字符串转换为正确的 DOM 元素呢?
令人惊讶的是,上面的代码在 FF 和 IE 中工作,但在 Chrome 中失败,当我尝试提交表单时
Uncaught TypeError: Cannot call method 'submit' of null
,因为字符串没有转换为实际的 DOM 元素
EDIT
alert(r.form); //Prints the form OK
$('my_div').set('html',r.form);
alert($('my_div').innerHTML); //Prints the form without the <form> tags..
仅在 Chrome 中!
修复
毕竟我试图将一个表单插入到另一个表单内的div中,这显然在Chrome上不被接受
My Mootools function gets a JSON object which is an html form in String format
var req = new Request.JSON({
method: 'post',
url: 'index.php',
onSuccess: function(r) {
if (typeof(r)!='undefined' && r!=null)
{
$('my_div').set('html',r.form);
}
});
Is there a way to convert the string response to an HTML element so I can later do:
document.myForm.submit();
I know that I can parse each and every form element and create the DOM elements one by one, but is there an easier way to just convert the form from string to a proper DOM element?
Surprisingly enough the above code works in FF and IE but fails in Chrome when I try to submit the form with
Uncaught TypeError: Cannot call method 'submit' of null
since the string is not converted to an actual DOM element
EDIT
alert(r.form); //Prints the form OK
$('my_div').set('html',r.form);
alert($('my_div').innerHTML); //Prints the form without the <form> tags..
Only in Chrome!
FIX
after all I was trying to insert a form into a div which was inside another form, this is not accepted on Chrome apparently
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将表单插入 DOM 后,您必须选择该表单:
You'll have to select the form after you inserted it in the DOM: