我对此表单验证有一些问题。现在,我的代码是这样的:
@model MassiveGenerationModel
<form id="FormGenerateMassive" name="FormGenerateMassive">
@Html.HiddenFor(model => model.DiscountId)
<table id="tableMassiveGeneration">
<tr>
@Html.LabelFor(model => model.CouponPrefix)
@Html.EditorFor(model => model.CouponPrefix)
@Html.ValidationMessageFor(model => model.CouponPrefix)
</tr>
<tr>
@Html.LabelFor(model => model.CouponNumber)
@Html.EditorFor(model => model.CouponNumber)
@Html.ValidationMessageFor(model => model.CouponNumber)
</td>
</tr>
<tr>
@Html.LabelFor(model => model.CouponLength)
@Html.EditorFor(model => model.CouponLength)
@Html.ValidationMessageFor(model => model.CouponLength)
</td>
</table>
<input type="button" id="GenerateMassiveButton" value="Massive generation" onclick ="CreateMassiveSubmit()">
<div style="display:none" id="messageGM">
<span id="messageFromServer"></span>
</div>
</form>
<script language="javascript" type="text/javascript">
function CreateMassiveSubmit() {
$("#GenerateMassiveButton").prop("disabled", true);
$("div#messageGM").css("display", "none");
var CreateMassiveSubmitCall = function (data) {
$("div#messageGM").css("display", "block");
$("span#messageFromServer").text(data.Message);
$("#GenerateMassiveButton").prop("disabled", false);
}
var CreateMassiveSubmitError = function (err) {
$("#GenerateMassiveButton").prop("disabled", false);
alert(err.Message);
}
if ($("#FormGenerateMassive").valid()) {
$("#FormGenerateMassive").postFormAjax("/Discount/MassiveGenerationCoupons", CreateMassiveSubmitCall, CreateMassiveSubmitError);
}
}
$.fn.postFormAjax = function (url, success, error) {
var data = this.serializeObject();
$.ajax({
cache: false,
type: "POST",
url: url,
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: success,
error: error
});
}
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
var nameDest = this.name;
var nameSplited = nameDest.split(".");
if (nameSplited.length > 1) {
nameDest = nameSplited[1];
}
if (o[nameDest] !== undefined) {
if (!o[nameDest].push) {
o[nameDest] = [o[nameDest]];
}
o[nameDest].push(this.value || '');
} else {
o[nameDest] = this.value || '';
}
});
return o;
};
</script>
&#13;
但是jquery验证器返回错误:&#34;无法读取属性&#39;形式&#39;未定义&#34;。发生了什么?我不明白为什么如果所有属性都有价值,表格就无法验证自己。
提前致谢!