Mozilla form.submit()不起作用

时间:2013-07-31 11:02:24

标签: javascript html dom mozilla

我正在使用以下代码

创建动态表单
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中)。 此代码由另一个函数调用,该函数由click事件上的按钮调用。 执行此代码后,我返回false。

请帮帮我。 在此先感谢: - )

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:-1)

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