2013-04-02 186 views
0

我有一个页面有窗体。我有一个window.setTimeout将调用一个触发提交按钮的函数。超时设置为10秒。但是,如果时间已过,该函数将被调用,但表单未提交。看起来提交按钮没有提交表单。请帮助如何使此功能起作用。我希望如果函数“SubmitForm”被调用,它会提交按钮而不点击提交按钮。以下是我的代码。通过trigerring提交表单jQuery提交按钮不起作用

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:/产品/创建

[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

尝试单击而不是提交。

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

完美!谢谢 –

3

#btnSubmit是你的提交按钮,而不是你的形式。尝试提交按钮是没有意义的。您可能的意思是:

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

要添加到这一点,从我读过,如果你提交一个表单,它可能是使用“提交”,而不是“点击”,只是因为不同的浏览器不同的工作更明智的选择,并按下“输入”键也可能提交。 – Zhouster