jQuery表单验证程序可以与Html.Beginform一起正常工作。在MVC中使用Ajax.BeginForm的onBegin方法中的jQuery表单验证程序
我将Html.Beginform更改为Ajax.Beginform以提交没有刷新的表单(尝试Ajax post但是文件在控制器中为null)。
更改为Ajax.Beginform后,jQuery表单验证程序不起作用。
是否可以在Ajax.Beginform的onBegin方法内使用jQuery表单验证器?
查看:
<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script src="~/scripts/jquery.validate.min.js"></script>
<script src="~/scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/scripts/jquery.unobtrusive-ajax.min.js"></script>
@using (Ajax.BeginForm("Index", "Home", new AjaxOptions { OnSuccess = "Success", OnFailure = "Failure", OnBegin = "validate", HttpMethod = "post" }, new { @Id = "form", enctype = "multipart/form-data" }))
{
<script>
function Success(data) {
alert(data);
}
function Failure() {
alert("failure");
}
function validate() {
$(document).ready(function() {
$("#form").validate({
rules: {
text1: {
required: true,
},
text2: {
required: true,
}
},
messages: {
text1: {
required: "enter text1"
},
text2: {
required: "enter text2"
},
}
});
if ($("#form").valid()) {
return true;
}
else {
return false;
}
});
}
</script>
<div>
<input type="text" id="text" name="text" />
</div>
<div>
<input type="text" id="text1" name="text1" />
</div>
<div>
<input type="submit" id="submit" name="submit" />
</div>
}
控制器:
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(FormCollection formcollection)
{
return Json("1");
}
}
是的,它是可能的,但你必须向我们展示一些代码 – GGO
现在检查@GGO ......如果我使用jQuery的内部验证onBegin函数抛出下面的错误。 – Yashas
0x800a01b6 - JavaScript运行时错误:对象不支持属性或方法'验证' – Yashas