Mozilla form.submit() not working

谁说我不能喝 提交于 2019-12-08 08:26:37

问题


I am creating a dynamic form using following code,

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();

}

But in Mozilla nothing happens but code works as expected ( in chrome). This code is being called by another function which is invoked by button on click event. After executing this code i am returning false.

Please help me out. Thanks in advance :-)


回答1:


You need to append the new created form to the document, because it was not there on page load.

Try this:

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();
}



回答2:


I had similar error just resolved the same.
If you have just used <form></form> tag and trying to submit then it gives error in older version of mozill while it works in newer version and other browsers.
The form tag should be under <html><body> tag. e.g. <html><body><form></form></body></html>



来源:https://stackoverflow.com/questions/17968565/mozilla-form-submit-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!