【问题标题】:Submit form through triggering jQuery submit button not working通过触发jQuery提交按钮提交表单不起作用
【发布时间】:2013-04-02 08:01:14
【问题描述】:

我有一个有表单的页面。我有一个 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/创建

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

【问题讨论】:

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


    【解决方案1】:

    #btnSubmit 是您的提交按钮,而不是您的表单。尝试提交按钮几乎没有意义。您可能的意思是:

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

    【讨论】:

    • 补充一点,从我读过的内容来看,如果您正在提交表单,使用“提交”而不是“点击”可能是一个更明智的选择,因为不同的浏览器可以工作不同的是,按“回车”键也可能提交。
    【解决方案2】:

    尝试点击而不是提交。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 2015-10-10
      相关资源
      最近更新 更多