通过触发jQuery提交按钮提交表单不起作用 [英] Submit form through triggering jQuery submit button not working

查看:170
本文介绍了通过触发jQuery提交按钮提交表单不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有表格的页面.我有一个window.setTimeout,它将调用触发提交按钮的函数.超时设置为10秒.但是,如果时间已过,则会调用该函数,但不会提交表单.似乎提交"按钮未提交表单.请帮助如何使此功能起作用.我希望如果调用函数"SubmitForm",它将在不单击提交按钮的情况下提交按钮.下面是我的代码.

I have a page that has form. I have a window.setTimeout that will call a function that trigger the submit button. The timeout is set to 10 seconds. However if the time has elapsed the function is called but the form is not submitted. It seems the submit button is not submitting the form. Please help how to make this function work. I want that if the function "SubmitForm" is called it will submit the button without clicking the submit button. Below is my code.

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

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

推荐答案

尝试单击而不是提交.

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

这篇关于通过触发jQuery提交按钮提交表单不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆