Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在加载时提交MVC视图?_Javascript_Asp.net Mvc_Forms_Asp.net Mvc 4_Form Submit - Fatal编程技术网

Javascript 如何在加载时提交MVC视图?

Javascript 如何在加载时提交MVC视图?,javascript,asp.net-mvc,forms,asp.net-mvc-4,form-submit,Javascript,Asp.net Mvc,Forms,Asp.net Mvc 4,Form Submit,我有一个视图,它有一个带有单个隐藏提交按钮的表单。我想在页面加载时提交表单。但是,我在Javascript中进行的任何提交都会导致以下错误: 无法获取未定义或空引用的属性“submit” 这是我的密码: PrintApplication.cshtml @using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-ho

我有一个视图,它有一个带有单个隐藏提交按钮的表单。我想在页面加载时提交表单。但是,我在Javascript中进行的任何提交都会导致以下错误:

无法获取未定义或空引用的属性“submit”

这是我的密码:

PrintApplication.cshtml

    @using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
    {
      <input id="btnPrintApplications" type="submit" class="btn btn-primary" value="PrintApplications" name="submit" style="visibility: hidden" />
    }

<script>

    document.onreadystatechange = function () {
        document.form.submit();
    }

</script>
以及其他变体(例如,在表单名称前面加#)

我的代码有什么问题。是否有其他方式自动提交页面(用户无需单击任何按钮)

编辑:这是我的原始代码。它在IE中不起作用,这正是引发我问题的原因。没有错误,但是表单没有提交。这在Chrome中运行良好

    $(document).ready(function () {
    $("#btnPrintApplications").click();
    });
编辑2:这是我使用@barry帖子中的代码得到的代码

@using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
{
    @*<input id="btnPrintApplications" type="submit" class="btn btn-primary" value="PrintApplications" name="submit" style="visibility: hidden" />*@
    <div>
        If applications do not appear in a separate tab or window, please click the button below.
    </div>
    <input id="btnPrintApplications" type="submit" class="btn btn-primary" value="Print Applications" name="print" />
}

<script>
  mySubmitButton = document.getElementById("btnPrintApplications");
    mySubmitButton.click();
</script>
@使用(Html.BeginForm(“PrintApplications”、“Admin”、FormMethod.Post、new{@id=“PrintApplications”、@class=“form horizontal”、@commandname=“ModelContract”、@target=“\u blank”}))
{
@**@
如果应用程序未出现在单独的选项卡或窗口中,请单击下面的按钮。
}
mySubmitButton=document.getElementById(“btnPrintApplications”);
mySubmitButton.click();
编辑3:下面是使用@chris pratt示例的代码

@using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
{
    <div>
        If applications do not appear in a separate tab or window, please click the button below.
    </div>
    <input id="btnPrintApplications" type="submit" class="btn btn-primary" value="Print Applications" name="print" />
}

<script>
    window.onload = function () {
        document.forms['PrintApplications'].submit();
    }
</script>
@使用(Html.BeginForm(“PrintApplications”、“Admin”、FormMethod.Post、new{@id=“PrintApplications”、@class=“form horizontal”、@commandname=“ModelContract”、@target=“\u blank”}))
{
如果应用程序未出现在单独的选项卡或窗口中,请单击下面的按钮。
}
window.onload=函数(){
document.forms['PrintApplications'].submit();
}

以下简单的HTML文档功能完全符合您的需要:

<html>
<body>
    <form id="Test" action="http://google.com" target="_blank">
        <input type="hidden" name="q" value="test" />
    </form>
    <script>
        window.onload = function () {
            document.forms['Test'].submit();
        }
    </script>
</body>
</html>

window.onload=函数(){
document.forms['Test'].submit();
}
我最初使用的是
document.onreadystatechange
,它也可以工作,但我注意到IE11显然触发了该事件两次,导致两个弹出窗口。不管也许通过反向工作,你可以找到你的问题。

试试这种方法

mySubmitButton = document.getElementById("PerfSubmit");         
mySubmitButton.click();         
使用下面的代码,表单将被提交

$('form')[0].submit();


您是否尝试过使用类似于
document.onload
?虽然它们在大多数情况下在功能上应该是等效的,
readystatechange
在文档的“准备就绪”发生更改时触发,不管这意味着什么,而
load
仅在元素和所有依赖项都已完全加载时才显式触发。实际上,请尝试将提交按钮命名为“提交”以外的名称. MDN似乎暗示这可能有问题:谢谢@chris pratt的反馈。刚刚尝试了document.onload并将输入按钮重命名为“print”而不是“submit”。两者都不起作用,但这次我没有出错。还有其他建议吗?谢谢@barry。这很有效。我使用这种方法在我的原始帖子中添加了准确的代码。谢谢@chris pratt。这也起了作用。实际上,我以为我尝试了window.onload,所以我怀疑是否会再次尝试,但我一定是弄错了一些语法,因为您的示例有效。谢谢你的帮助!
$('form')[0].submit();
mySubmitButton.trigger("click");