【问题标题】:jquery .valid() not working when resending the same formjquery .valid() 在重新发送相同的表单时不起作用
【发布时间】:2019-08-03 11:28:07
【问题描述】:

我觉得这很简单,但我想不通,

我有一个简单的表格,要求输入姓名、日期一和日期二。

当你提交这里的 jquery

    $('#searchRange').on('submit', function (e) {
        e.preventDefault()
        var x = $(this).serialize();
        if ($(this).valid()) {
            $.ajax({
              type: 'POST',
              url: '/Statistics/Home',
              data: x,
              cache: false,
              beforeSend: function () {
                  $('.dk-linear-loading-wrapper').fadeIn('slow');
              },
              success: function (e) {
                  $('#sellsTable').html(e);
              },
              complete: function () {
                  $('.dk-linear-loading-wrapper').fadeOut();
              }
           });
        }
    })

ajax 返回一个插入到 div 中的表,到此为止没问题。

但是现在当我修改表单更改一些值并单击提交按钮重新发送表单时,这是我从控制台得到的:

未捕获的类型错误:$(...).valid 不是函数

有人知道为什么吗?我该如何解决?

谢谢。

更新

这是 bundleconfig 文件:

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        bundles.Add(new ScriptBundle("~/bundles/custom-validators").Include(
                    "~/Scripts/custom-validators.js"));

        bundles.Add(new ScriptBundle("~/bundles/myScripts").Include(
                    "~/Scripts/Plugin.js",
                    "~/Scripts/Velocity.js",
                    "~/Scripts/Chart.js",
                    "~/Scripts/Admin.js"));

这是表格:

<form id="searchRange">
<label class="col-sm-auto text-center">
            <i class="fas fa-user"></i>
            Nombre:
        </label>
        <div class="col-sm-2">
            <div class="form-group row">
                @Html.TextBoxFor(model => model.name, new { @class = "form-control form-control-sm text-center mr-3" })
            </div>
            <div class="row">
                @Html.ValidationMessageFor(model => model.name, "", new { @class = "alert alert-danger", type = "date" })
            </div>
        </div>
        <label class="col-sm-auto text-center">
            <i class="fas fa-calendar-alt"></i>
            Fecha desde:
        </label>
        <div class="col-sm-2">
            <div class="form-group row">
                @Html.TextBoxFor(model => model.dateFrom, new { @class = "form-control form-control-sm text-center mr-3" })
            </div>
            <div class="row">
                @Html.ValidationMessageFor(model => model.dateFrom, "", new { @class = "alert alert-danger", type = "date" })
            </div>
        </div>
        <label class="col-sm-auto text-center">
            <i class="fas fa-calendar-alt"></i>
            Fecha hasta:
        </label>
        <div class="col-sm-2">
            <div class="form-group row">
                @Html.TextBoxFor(model => model.dateUntil, new { @class = "form-control form-control-sm text-center mr-3" })
            </div>
            <div class="row">
                @Html.ValidationMessageFor(model => model.dateUntil, "", new { @class = "alert alert-danger", type = "date" })
            </div>
        </div>
        <div class="col-sm-auto">
            <div class="form-group">
                <input type="submit" class="btn btn-primary btn-sm" value="Buscar" />
            </div>
        </div>
</form>

更新

这是我更改提交的方式

     $('#searchPositionsByCode').validate({
        submitHandeler: function (form) {
            $.ajax({
                type: $(form).attr("method"),
                url: $(form).attr("action"),
                data: $(form).serialize(),
                cache: false,
                beforeSend: function () {
                    $('.dk-linear-loading-wrapper').fadeIn('slow');
                },
                success: function (e) {
                    $('#infoByCodeDisplayed').html(e);
                    $('#infoByCodeDisplayed').removeClass('d-none');
                },
                complete: function () {
                    $('.dk-linear-loading-wrapper').fadeOut();
                }
            })
        return false; 
        }
    });

但是这种方式使用默认的提交 jquery 验证,它不执行 ajax,并将我重定向到另一个页面。

有什么想法吗?

【问题讨论】:

  • 能否请您出示您的表格。
  • 你做错了
  • @KrishnaJonnalagadda 怎么样?
  • .valid() 方法只能在.validate() 方法之后调用。 .validate() 方法由作为 ASP 的一部分的 Unobtrusive Validation 插件自动创建。你必须检查你的 DOM 以确保你的时间是正确的。使用 jQuery Validate 插件时,提交按钮的点击事件会被自动捕获,所以你绝对没有理由创建submit 事件处理函数。
  • 如果您将 Unobtrusive Validation 插件用作 ASP 的一部分,那么您对 ​​.validate() 的调用将被忽略.validate() 方法只能被调用一次,并且不显眼的验证将优先。

标签: c# jquery asp.net-mvc unobtrusive-validation


【解决方案1】:

你需要在 jQuery 下面的 jQuery 有效插件才能工作

<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>

您也可以结帐jQuery validate()

【讨论】:

    【解决方案2】:

    **解决了它**

    @Sparky 推荐了一种更好的方法,这样做仍然有效

        $('#searchRange').data('validator').settings.submitHandler = function (form) {
            $.ajax({
                type: 'POST',
                url: '/Statistics/Home',
                data: $('#searchRange').serialize(),
                cache: false,
                beforeSend: function () {
                    $('.dk-linear-loading-wrapper').fadeIn('slow');
                },
                success: function (e) {
                    $('#sellsTable').html(e);
                },
                complete: function () {
                    $('.dk-linear-loading-wrapper').fadeOut();
                }
            })
    

    【讨论】:

    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多