Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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
通过触发jQuery提交按钮提交表单不起作用_Jquery_Asp.net Mvc 3_Form Submit - Fatal编程技术网

通过触发jQuery提交按钮提交表单不起作用

通过触发jQuery提交按钮提交表单不起作用,jquery,asp.net-mvc-3,form-submit,Jquery,Asp.net Mvc 3,Form Submit,我有一个有格式的页面。我有一个window.setTimeout,它将调用触发submit按钮的函数。超时设置为10秒。但是,如果时间已过,则调用函数,但不提交表单。“提交”按钮似乎没有提交表单。请帮助如何使此功能正常工作。我想,如果函数“SubmitForm”被调用,它将提交按钮而不单击提交按钮。下面是我的代码 HTML @model MVCViewState.Models.Product <!DOCTYPE html> <html> <head>

我有一个有格式的页面。我有一个window.setTimeout,它将调用触发submit按钮的函数。超时设置为10秒。但是,如果时间已过,则调用函数,但不提交表单。“提交”按钮似乎没有提交表单。请帮助如何使此功能正常工作。我想,如果函数“SubmitForm”被调用,它将提交按钮而不单击提交按钮。下面是我的代码

HTML

@model MVCViewState.Models.Product
<!DOCTYPE html>
<html>
<head>
    <title>Create</title>
    <script type="text/javascript">
        window.setTimeout(SubmitForm, 10000);
        function SubmitForm() {
            alert('called');
            $('#btnSubmit').submit();
        }
    </script>
</head>
<body>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    @using (Html.BeginForm("Create", "Products", FormMethod.Post, new { id = "ProductForm" }))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Product</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Description)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Description)
                @Html.ValidationMessageFor(model => model.Description)
            </div>
            <p>
                <input type="submit" value="Create" id="btnSubmit"/>
            </p>
        </fieldset>
    }
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
</body>
</html>
@model MVCViewState.Models.Product
创造
setTimeout(提交格式,10000);
函数SubmitForm(){
警报(“被呼叫”);
$('btnSubmit').submit();
}
@使用(Html.BeginForm(“Create”、“Products”、FormMethod.Post、new{id=“ProductForm”}))
{
@Html.ValidationSummary(true)
产品
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.Description)
@EditorFor(model=>model.Description)
@Html.ValidationMessageFor(model=>model.Description)

} @ActionLink(“返回列表”、“索引”)
控制器

// //发布:/Products/Create

    [HttpPost]
    public ActionResult Create(Product product)
    {
        try
        {
            List<Product> products;
            if (Session["products"] != null) products = Session["products"] as List<Product>;
            else products = new List<Product>();
            if (products != null) products.Add(product);
            product.Id = products.Max(x => x.Id) + 1;
            Session["products"] = products;
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
[HttpPost]
公共行动结果创建(产品)
{
尝试
{
列出产品清单;
如果(会话[“产品”!=null)产品=会话[“产品”]作为列表;
else products=新列表();
如果(products!=null)products.Add(product);
product.Id=products.Max(x=>x.Id)+1;
会话[“产品”]=产品;
返回操作(“索引”);
}
抓住
{
返回视图();
}
}
#btnSubmit
是您的提交按钮,而不是表单。尝试提交按钮没有什么意义。你可能是说:

$('#ProductForm').submit();
#btnSubmit
是您的提交按钮,而不是表单。尝试提交按钮没有什么意义。你可能是说:

$('#ProductForm').submit();

尝试单击而不是提交

$('#btnSubmit').click();

尝试单击而不是提交

$('#btnSubmit').click();

此外,据我所知,如果您提交表单,使用“提交”而不是“单击”可能是一个更明智的选择,因为不同的浏览器工作方式不同,按“回车”键也可能提交。此外,据我所知,如果您提交表单,使用“提交”可能是一个更明智的选择而不是“点击”,只是因为不同的浏览器工作方式不同,按“回车”键也有可能提交。