【问题标题】:JS function returning an error valid() is not a function?返回错误valid()的JS函数不是函数吗?
【发布时间】:2020-04-17 05:20:01
【问题描述】:

我正在使用 jquery 验证库并想在 ajax 调用之前检查验证错误,但是

我收到一个错误,valid() 不是函数

我也在使用下面给出的 jquery 验证库。我的表单 ID 是共享的,我使用两个不同的 ajax 调用,一个用于行更新,另一个用于批量更新。

   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"></script>
var RowId = 0;
    var tagvalue = 0;
        function UpdateRow(id)
        {
            tagvalue = $("#TagVaule_" + id).val();
            RowId = id;
            if ($("#share").valid())
            {
                DisplayModal();
            }
            else
            {
                return false;
            }
        }

        function DisplayModal()
        {

             $.ajax({
                type: "GET",
                 url: '@Url.Action("Update","Home")',
                data: {
                    id: RowId,
                    value: tagvalue
                },
                success: function(data)
            {
                $('#myModalContent').html(data);
                $('#myModal').modal('show');
            }
            });

        }
fORM
<form id="share">
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="container col-md-12">
    <table id="myTable" class="table table-hover table-striped table-bordered dataTable">
        <thead>
            <tr>
                <th style="text-align:center">@Html.DisplayNameFor(m => Model.tags.First().Id)</th>
                <th style="text-align:center">@Html.DisplayNameFor(m => Model.tags.First().TagName)</th>
                <th style="text-align:center">@Html.DisplayNameFor(m => Model.tags.First().TagCategory)</th>
                <th style="text-align:center">@Html.DisplayNameFor(m => Model.tags.First().TagValue)</th>
                <th style="text-align:center"> Action</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.tags.Count(); i++)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(m => Model.tags[i].Id)
                        @Html.HiddenFor(m => Model.tags[i].Id)
                    </td>
                    <td>
                        @Html.DisplayFor(m => Model.tags[i].TagName)
                    </td>
                    <td>
                        @Html.DisplayFor(m => Model.tags[i].TagCategory)
                    </td>
                    <td>
                        @Html.EditorFor(m => Model.tags[i].TagValue, new { htmlAttributes = new { @id = "TagVaule_" + Model.tags[i].Id, @class = "form-control",required="required" } })
                        @Html.ValidationMessageFor(m => Model.tags[i].TagValue, "", new { @class = "text-danger" })

                    </td>
                    <td>
                        <button type="button" class="btn btn-danger" onclick="UpdateRow(@Model.tags[i].Id)">Update</button>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content" id="myModalContent">

            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-5 col-md-10">
            <button type="button" class="btn btn-danger"   onclick="BulkUpdate()">BulkUpdate</button>
        </div>
    </div>
</div>

【问题讨论】:

  • 也粘贴你的html代码
  • #share 你的form id?

标签: javascript ajax asp.net-mvc validation


【解决方案1】:

该页面可能尚未插入 jQuery 的 validator 库。请在jQuery的&lt;script&gt;标签后面插入下面一行:-

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>

在执行任何valid()之前,您需要执行一次表单对象validate()

例如:-

$(document).ready(function(){
    $("form").validate(); // can use id or class as form selector
});

【讨论】:

  • Ronnie Tws 你能详细说明一下吗?
  • 未捕获的类型错误:$(...).validate 不是函数
  • 我认为您的验证器插件无效,我已经更新了答案。请检查一下。
  • Ronnie Tws 还是同样的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 2015-12-09
  • 2016-10-08
  • 2020-05-09
  • 1970-01-01
  • 2021-03-30
  • 2018-06-08
相关资源
最近更新 更多