我有一個頁面有窗體。我有一個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();
}
}
完美!謝謝 –