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

时间:2013-04-02 08:01:15

标签: jquery asp.net-mvc-3 form-submit

我有一个有形式的页面。我有一个window.setTimeout,它将调用一个触发提交按钮的函数。超时设置为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>

控制器

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

2 个答案:

答案 0 :(得分:3)

#btnSubmit是您的提交按钮,而不是您的表单。尝试提交按钮毫无意义。你可能意味着:

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

答案 1 :(得分:2)

尝试点击而不是提交。

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