簡體   English   中英

如何讓jQuery驗證成功函數以對隱藏的輸入字段進行操作?

[英]How to get jQuery validate success function to operate on a hidden input field?

我有一個正在通過jQuery.validate驗證的表單。 使用errorPlacement選項,我具有一組用於實時錯誤驗證的自定義函數。 使用成功選項,我有一組自定義函數來消除和替換錯誤消息。

除了我對隱藏字段的驗證僅適用於提交外,所有方法都很好。 使用成功選項,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');
                    }
                },

我的用例有些不同,因為我沒有使用自定義處理程序來獲得成功。 但是,我遇到了一個問題,即更改隱藏字段后錯誤不會消失。 我的意思是說我正在使用默認的錯誤和成功功能,但應該沒有任何區別,您的處理程序仍應運行。

似乎最簡單的方法是調用.form()方法。 因此,在填充(或清空)隱藏輸入的事件中,我簡單地擁有以下內容:

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

輸入是事件中$()大小的元素,然后向上遍歷以獲取表單(驗證程序附加到該窗體),然后驗證程序,然后調用.form()。 這將重新測試表單,在適用的情況下清除/添加錯誤,但不提交。

希望有幫助,詹姆斯

好吧...對此沒有答案...

但是我很確定這是jQuery.validate缺少的功能(隱藏字段中的更改不會觸發成功和errorPlacement事件)。 我通過在jQuery change事件內復制成功和errorPlacement下的代碼來解決此問題,如下所示:

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

如果您使用的是動態事件和錯誤消息,則可能...您必須用與要編碼的$('selector')相關的靜態替代方法替換該代碼。

希望能對某人有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM