jQuery 验证 - 元素未定义

发布于 2025-01-07 03:55:33 字数 1251 浏览 5 评论 0原文

我正在使用 jquery.validation 插件来验证我在表单上的输入

,如下所示:

$(document).ready(function () {
jQuery(function () {
    // You can specify some validation options here but not rules and messages

    jQuery('form[id="reservationForm"]').validate({ ignore: ":not(:visible)" });

    $("#adult1Birthdate").mask("9999-99-99");
    $("#reservationPersonBirthdate").mask("9999-99-99");

    $("#adult1Firstname").rules("add", { required: true, minlength: 2, messages: { required: '<span style="color:Red">Pole wymagane</span>'} });
});

});

但是我收到以下错误:

元素未定义

以下行中的

var settings = $.data(element.form, 'validator').settings;

:我做错了什么?

**EDIT**

当我使用 jquery.validation 时,以下代码也不起作用

    $(document).ready(function () {
$(function () {

                $('a[id^="regionDetails"]').click(function () {
                    var vid_id = $(this).attr("id").replace("regionDetails", "#regionDetailsArea");
                    $('div[id^="regionDetailsArea"]').hide();
                    $(vid_id).show();
                    return false;
                });
            });
});

I am using a jquery.validation plugin to validate my inputs on the form

I use it as follows:

$(document).ready(function () {
jQuery(function () {
    // You can specify some validation options here but not rules and messages

    jQuery('form[id="reservationForm"]').validate({ ignore: ":not(:visible)" });

    $("#adult1Birthdate").mask("9999-99-99");
    $("#reservationPersonBirthdate").mask("9999-99-99");

    $("#adult1Firstname").rules("add", { required: true, minlength: 2, messages: { required: '<span style="color:Red">Pole wymagane</span>'} });
});

});

I get the following error however:

element is undefined

in the following line:

var settings = $.data(element.form, 'validator').settings;

What am I doing wrong?

**EDIT**

Also the following code does not work when I use jquery.validation

    $(document).ready(function () {
$(function () {

                $('a[id^="regionDetails"]').click(function () {
                    var vid_id = $(this).attr("id").replace("regionDetails", "#regionDetailsArea");
                    $('div[id^="regionDetailsArea"]').hide();
                    $(vid_id).show();
                    return false;
                });
            });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷血 2025-01-14 03:55:33

确保您调用 .rules() 的元素确实存在,jquery.validate 在添加规则时不会检查它,并且在所描述的行处惨败,只是因为该元素未定义。

Make sure the element you're calling .rules() on does exist, jquery.validate doesn't check for it when adding rules and fails miserably at the described line, simply because the element is not defined.

娇俏 2025-01-14 03:55:33

如果您使用 $,请勿使用 jQuery。试试这个:

$(document).ready(function () {
  $('#reservationForm:visible').validate();

  $("#adult1Birthdate").mask("9999-99-99");
  $("#reservationPersonBirthdate").mask("9999-99-99");

  $("#adult1Firstname").rules("add", { required: true, minlength: 2, messages: { required: '<span style="color:Red">Pole wymagane</span>'} });

  $('a:not(#regionDetails)').click(function() {
    $('div:not(#regionDetailsArea)').hide();
    $($(this).attr("id").replace("regionDetails", "#regionDetailsArea")).show();

    return false;
  });
});

If you're using $, don't use jQuery. Try this:

$(document).ready(function () {
  $('#reservationForm:visible').validate();

  $("#adult1Birthdate").mask("9999-99-99");
  $("#reservationPersonBirthdate").mask("9999-99-99");

  $("#adult1Firstname").rules("add", { required: true, minlength: 2, messages: { required: '<span style="color:Red">Pole wymagane</span>'} });

  $('a:not(#regionDetails)').click(function() {
    $('div:not(#regionDetailsArea)').hide();
    $($(this).attr("id").replace("regionDetails", "#regionDetailsArea")).show();

    return false;
  });
});
硬不硬你别怂 2025-01-14 03:55:33

我注意到,当我出现此错误时,我在表单中未定义的规则中调用了一个输入。或者,反之亦然。

I've noticed that when this error appears for me, I have an input being called in rules that I don't have defined in my form. Or, vice versa.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文