Javascript 如何使 document.forms[0].submit() 工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5591133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:57:36  来源:igfitidea点击:

how to make document.forms[0].submit() work?

javascriptjqueryforms

提问by pivotal developer

After i implemented my dialog box as a alert measure for user to confirm submission instead of a direct submission upon clicking, my document.forms[0].submit() does not work anymore. The page refreshes and the page stays the same. Before that dialog box was created, everything works perfectly. what is the problem here?

在我实现对话框作为用户确认提交而不是单击时直接提交的警报措施后,我的 document.forms[0].submit() 不再起作用。页面刷新,页面保持不变。在创建该对话框之前,一切正常。这里有什么问题?

After:

后:

  <script>
   //trigger dialog
 $('#submitIter').click(function(){
    $("#dialog-submit").dialog("open");
                return false
});

$("#dialog-submit").dialog({            
    autoOpen: false,
    resizable: false,
    height: 200,
    width: 200,
    modal: true,
    buttons: {
        "Proceed": function(){
            //submit after user clicks proceed cannot seem to work
            document.forms[0].submit();
            $(this).dialog("close");

        }
    }
});
   </script>

    <form action="{{ request.path }}" method="post" enctype="multipart/form-data">

      <input type="image" src="images/submit.png" id="submitIter">

    </form>

Before:

前:

<script> document.forms[0].submit() </script>

采纳答案by Arthur Halma

I think you add some form before this one, try to write document.forms[1].submit();Anyway as I can see you using jQuery, so you can write it this way:

我想你在这个之前添加了一些表单,尝试编写document.forms[1].submit(); 无论如何,我可以看到你使用 jQuery,所以你可以这样写:

<script>
   //trigger dialog
 $('#submitIter').click(function(){
    $("#dialog-submit").dialog("open");
                return false
});

$("#dialog-submit").dialog({            
    autoOpen: false,
    resizable: false,
    height: 200,
    width: 200,
    modal: true,
    buttons: {
        "Proceed": function(){
            //submit after user clicks proceed cannot seem to work
            $('#form_for_submit').submit();
            $(this).dialog("close");

        }
    }
});
   </script>

    <form id="form_for_submit" action="{{ request.path }}" method="post" enctype="multipart/form-data">

      <input type="image" src="images/submit.png" id="submitIter">

    </form>

回答by Andrew

It may have something to do with scoping or potentially another form being added by the dialog. My first debugging step would be to wrapp document.forms[0] into an alert and see if it returns an object. Secondly I would give the form tag an id to explicity reference it e.g. id="mainForm" then you can do document.getElementById("mainForm").submit();

它可能与作用域有关,或者可能与对话框添加的另一种形式有关。我的第一个调试步骤是将 document.forms[0] 包装成一个警报并查看它是否返回一个对象。其次,我会给表单标签一个 id 来明确引用它,例如 id="mainForm" 然后你可以做document.getElementById("mainForm").submit();

Or you could write it as the other answer suggests

或者你可以按照另一个答案的建议来写

$("#mainForm").submit();

Also try calling the dialog close command first.

也尝试先调用对话框关闭命令。