1

我正在使用以下代码创建动态表单,

function createForm() {
    var f = document.createElement("form");

    f.setAttribute('method',"post");
    f.setAttribute('action',"./Upload");
    f.setAttribute('name',"initiateForm");
    f.acceptCharset="UTF-8";

    var name = document.createElement("input");
    name.setAttribute('type',"text");
    name.setAttribute('name',"projectname");
    name.setAttribute('value',"saket");

    f.appendChild(name);

    f.submit();

}

但是在 Mozilla 中什么都没有发生,但代码按预期工作(在 chrome 中)。此代码由另一个函数调用,该函数由单击事件上的按钮调用。执行此代码后,我返回 false。

请帮帮我。提前致谢 :-)

4

2 回答 2

2

您需要将新创建的表单附加到文档中,因为它在页面加载时不存在。

尝试这个:

function createForm() {
    var f = document.createElement("form");

    f.setAttribute('method',"post");
    f.setAttribute('action',"./Upload");
    f.setAttribute('name',"initiateForm");
    f.acceptCharset="UTF-8";

    var name = document.createElement("input");
    name.setAttribute('type',"text");
    name.setAttribute('name',"projectname");
    name.setAttribute('value',"saket");

    f.appendChild(name);
    document.body.appendChild(f); // added this
    f.submit();
}
于 2013-07-31T11:07:21.743 回答
-1

我有类似的错误只是解决了同样的问题。
如果您刚刚使用<form></form>标签并尝试提交,那么它在旧版本的 mozill 中会出现错误,而它在新版本和其他浏览器中工作。
表单标签应该在<html><body>标签下。例如<html><body><form></form></body></html>

于 2014-02-24T08:28:41.190 回答