Error validating form with jQuery

◇◆丶佛笑我妖孽 提交于 2019-12-31 05:41:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!