使用jQuery验证表单时出错

使用jQuery验证表单时出错

本文介绍了使用jQuery验证表单时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试验证表单时出现以下错误:

I'm getting the following error when trying to validate my form:

'this.0.form' is null or not an object

我正在使用最新的jQuery和jQuery.Validate,以及jQuery.Validate.Unobtrusive(通过NuGet).

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();
     }
.
.
.

推荐答案

您的jQuery选择器省略了#符号. jQuery ID选择器应该包含如下所示的#:

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"
 }

否则,您将valid()方法应用于空的jQuery对象,这将导致您指定的错误.

Otherwise you are applying the valid() method to an empty jQuery object, which results with the error you've specified.

这篇关于使用jQuery验证表单时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:07