通过trigerring jQuery的提交按钮不灵提交表单 [英] Submit form through trigerring jQuery submit button not working

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

问题描述

我有一个具有表单的页面。我有一个window.setTimeout,将调用触发提交按钮的功能。超时设置为10秒。但是,如果时间已过函数被调用,但不提交表单。看来提交按钮不提交表单。请帮助如何使这项工作的功能。我想,如果函数SubmitForm之称它将提交按钮而不点击提交按钮。下面是我的code。

HTML

  @model MVCViewState.Models.Product
<!DOCTYPE HTML>
< HTML和GT;
< HEAD>
    <标题>创建< /标题>
    <脚本类型=文/ JavaScript的>
        window.setTimeout(SubmitForm,10000);
        功能SubmitForm(){
            警报('称为');
            $('#btnSubmit按钮)提交()。
        }
    < / SCRIPT>
< /头>
<身体GT;
    <脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
    <脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
    @using(Html.BeginForm(创建,产品,FormMethod.Post,新{ID =ProductForm}))
    {
        @ Html.ValidationSummary(真)
        <&字段集GT;
            <传奇>产品与LT; /传说>            < D​​IV CLASS =编辑标记>
                @ Html.LabelFor(型号=> model.Name)
            < / DIV>
            < D​​IV CLASS =主编场>
                @ Html.EditorFor(型号=> model.Name)
                @ Html.ValidationMessageFor(型号=> model.Name)
            < / DIV>            < D​​IV CLASS =编辑标记>
                @ Html.LabelFor(型号=> model.Description)
            < / DIV>
            < D​​IV CLASS =主编场>
                @ Html.EditorFor(型号=> model.Description)
                @ Html.ValidationMessageFor(型号=> model.Description)
            < / DIV>
            &所述p为H.;
                <输入类型=提交值=创建ID =btnSubmit按钮/>
            &所述; / P>
        < /字段集>
    }
    < D​​IV>
        @ Html.ActionLink(返回目录,索引)
    < / DIV>
< /身体GT;
< / HTML>

控制器

//
        // POST:/产品/创建

  [HttpPost]
    公众的ActionResult创建(产品产品)
    {
        尝试
        {
            清单<产品与GT;产品;
            如果(会话[产品]!= NULL)产品=会话[产品作为列表与LT;产品取代;
            其他产品=新的List<产品及GT;();
            如果(产品!= NULL)products.Add(产品);
            product.Id = products.Max(X => x.Id)+ 1;
            会话[产品] =产品;
            返回RedirectToAction(「指数」);
        }
        抓住
        {
            返回查看();
        }
    }


解决方案

尝试用点击,而不是提交。

  $('#btnSubmit按钮')点击()。

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>

Controller

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

解决方案

Try with click instead of Submit.

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

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

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