如何让 jQuery 验证成功函数对隐藏的输入字段进行操作?

发布于 2024-09-10 09:33:04 字数 1773 浏览 0 评论 0原文

我有一个正在由 jQuery.validate 验证的表单。使用 errorPlacement 选项,我有一组自定义函数用于实时错误验证。使用 success 选项,我有一组自定义函数来消除和替换错误消息。

除了我对隐藏字段的验证仅在提交时有效之外,一切都很好。使用 success 选项,jQuery.validate 对于除我的隐藏字段之外的所有其他字段都表现良好。我使用隐藏字段来合并多个文本框和复选框中的值。下面是一个关于我的困境的示例,其中包含三个出生日期字段:日、月、年:

HTML:

<label for="birthmonth">Birthdate</label></div>
<div class="birthfield1"><input type="text" id="birthmonth" name="birthmonth" class="ignore" /></div>
<div class="birthfield23"><input type="text" id="birthday" name="birthday" class="ignore" /></div>
<div class="birthfield23"><input type="text" id="birthyear" name="birthyear" class="ignore" /></div>
<div class="errorparent"><input id="birthdate"  name="birthdate" type="hidden" /><div class="norederrorx errordetails2">&nbsp;</div></div>

用于更新隐藏出生日期字段的 jQuery/javascript:

    $('#birthday,#birthmonth,#birthyear').change(function() {
    var inputbirthday = $('#birthday').val();
    var inputbirthmonth = $('#birthmonth').val();
    var inputbirthyear = $('#birthyear').val();

    if (inputbirthday && inputbirthmonth && inputbirthyear) {
        alert('all values');
        $('#birthdate').val($('#birthday').val()+'/'+ $('#birthmonth').val()+'/'+ $('#birthyear').val());
    }
    if (inputbirthday == "" || inputbirthmonth == "" || inputbirthyear == ""){
        $('#birthdate').val('');
    }
});

以及 jQuery.validate 成功选项代码(有效:

success: function() {
  if (elementholder.hasClass('ignore') == false) {
  errorspotholder.find('.rederrorx').removeClass('rederrorx').addClass('norederrorx');
                    }
                },

I have a form being validated by jQuery.validate. Using errorPlacement option I have a custom set of functions for real-time error validation. Using success option I have a custom set of functions to get rid of and replace error messages.

All works great except my validation on hidden fields only works on submit. With the success option the jQuery.validate is performing perfectly for all other fields except for my hidden fields. I'm using hidden fields to consolidate values from multiple text boxes and check boxes. Here's an example on my dilemma with three birthdate fields: day, month, year:

The HTML:

<label for="birthmonth">Birthdate</label></div>
<div class="birthfield1"><input type="text" id="birthmonth" name="birthmonth" class="ignore" /></div>
<div class="birthfield23"><input type="text" id="birthday" name="birthday" class="ignore" /></div>
<div class="birthfield23"><input type="text" id="birthyear" name="birthyear" class="ignore" /></div>
<div class="errorparent"><input id="birthdate"  name="birthdate" type="hidden" /><div class="norederrorx errordetails2"> </div></div>

The jQuery/javascript to update the hidden birthdate field:

    $('#birthday,#birthmonth,#birthyear').change(function() {
    var inputbirthday = $('#birthday').val();
    var inputbirthmonth = $('#birthmonth').val();
    var inputbirthyear = $('#birthyear').val();

    if (inputbirthday && inputbirthmonth && inputbirthyear) {
        alert('all values');
        $('#birthdate').val($('#birthday').val()+'/'+ $('#birthmonth').val()+'/'+ $('#birthyear').val());
    }
    if (inputbirthday == "" || inputbirthmonth == "" || inputbirthyear == ""){
        $('#birthdate').val('');
    }
});

And the jQuery.validate success option code (which works:

success: function() {
  if (elementholder.hasClass('ignore') == false) {
  errorspotholder.find('.rederrorx').removeClass('rederrorx').addClass('norederrorx');
                    }
                },

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

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

发布评论

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

评论(2

温柔戏命师 2024-09-17 09:33:04

我的用例有点不同,因为我不使用自定义处理程序来获得成功。但是,我遇到了一个问题,即更改隐藏字段后错误不会消失。我的意思是,我正在使用默认的错误和成功功能,但不应该有任何区别——您的处理程序仍然应该运行。

处理此问题的最简单方法似乎是调用 .form() 方法。因此,在填充(或清空)隐藏输入的事件中,我只需这样:

input.parents('form').validate().form();

输入是 $() -ized 事件中的元素,然后向上遍历以获取表单(验证器附加到该表单),然后获取验证器,然后调用 .form()。这将重新测试表单,清除/添加适用的错误,但不提交。

希望有帮助,
詹姆斯

My use case is a bit different in that I don't use a custom handler for success. However, I was having an issue where the error wouldn't disappear after the hidden field was changed. What I'm saying is that I'm using the default error and success functionality, but there shouldn't be any difference -- your handler should still get run.

It seems the easiest way to handle this is to call the .form() method. So, within the event that fills (or empties) the hidden input, I simply have this:

input.parents('form').validate().form();

input being the $()-ized element from the event, then I traverse upward to get the form (which the validator was attached to), then the validator, then call .form(). This retests the form, clears/adds errors where applicable, but doesn't submit.

Hope that helps,
James

三月梨花 2024-09-17 09:33:04

好吧...这个问题没有答案...

但我很确定这是 jQuery.validate 的一项缺失功能(隐藏字段中的更改不会触发 success 和 errorPlacement 事件)。我通过复制 jQuery 更改事件内 success 和 errorPlacement 下的代码来解决这个问题,如下所示:

$('#chboxterms').change(function() {
   if ($('#chboxterms').is(':checked')) {
       //replicate jQuery.validate "success" code here
   } else {
       //replicate jQuery.validate "errorPlacement" code here
   }
}

如果您使用动态事件和错误消息,您可能是...您必须用静态替代代码替换该代码这与您正在编码的 $('selector') 相关。

希望对某人有帮助!

Okay...no answer on this one...

But I'm pretty sure it's a missing feature of jQuery.validate (that changes in hidden fields don't fire success and errorPlacement events). I got around it by replicating the code under success and errorPlacement inside of jQuery change events, like so:

$('#chboxterms').change(function() {
   if ($('#chboxterms').is(':checked')) {
       //replicate jQuery.validate "success" code here
   } else {
       //replicate jQuery.validate "errorPlacement" code here
   }
}

If you are using dynamic events and error messages, which you probably are...you'll have to replace that code with the static alternative that relates to the $('selector') that you're coding for.

Hope that helps someone!

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