问题
I'm getting the following error when trying to validate my form:
'this.0.form' is null or not an object
I'm using the latest jQuery and jQuery.Validate, and jQuery.Validate.Unobtrusive (via NuGet).
Here is the code. Where is the error coming from?
@model URIntake.IntakeFormViewModel
@{
ViewBag.Title = "Claim Information";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="SubmissionForm">
@using (Html.BeginForm("Save", "UrIntake", FormMethod.Post, new { id = "UrIntakeForm"}))
{
@Html.TextBoxFor(x => x.FormSubmitter.LastName)
@Html.TextBoxFor(x => x.FormSubmitter.FirstName)
<div id="SubmissionButtons" class="right">
<input type="button" onclick="SubmitForm()" value="Submit" />
<input type="button" onclick="CancelForm()" value="Cancel" />
</div>
}
</div>
<script type="text/javascript">
function CancelForm() {
document.location = "/";
}
function SubmitForm() {
$("UrIntakeForm").valid();
}
.
.
.
回答1:
Your jQuery selector for the form omits the #
sign. The jQuery ID selector should include a #
like so:
function SubmitForm() {
$("#UrIntakeForm").valid(); // "#UrIntakeForm" instead of "UrIntakeForm"
}
Otherwise you are applying the valid()
method to an empty jQuery object, which results with the error you've specified.
回答2:
Your jQuery selector is wrong
function SubmitForm() {
$("#UrIntakeForm").valid();
}
add a hash (#) to the selector to identify it as an id
来源:https://stackoverflow.com/questions/14821932/error-validating-form-with-jquery