2011-03-29 60 views
1

我已經使用jQuery.validator.addMethod來創建自定義驗證方法,該方法將確保我的數據條目表中至少有一行。使用Firebug,我的方法中的斷點沒有被擊中。我錯過了什麼嗎?jQuery驗證未調用的自定義方法

注意:我確定驗證方法本身存在問題,但那是因爲我還無法調試它。我仍然是JavaScript的新手。

更新:我已經知道它沒有被觸發,因爲我沒有在輸入字段上的驗證器。我現在有它的工作,但是,有沒有一種方法來添加驗證,而不將其附加到輸入字段?

這裏是我的表是什麼樣子:

<table id="editorRows"> 
    ... 
    <tbody class="editorRow"> 
     <tr class="row1"> 
     </tr> 
     <tr class="row2" style="display: none;"> 
     </tr> 
     <tr class="row3" style="display:none;"> 
     </tr> 
    </tbody> 
    <tbody class="editorRow"> 
     <tr class="row1"> 
     </tr> 
     <tr class="row2" style="display: none;"> 
     </tr> 
     <tr class="row3" style="display:none;"> 
     </tr> 
    </tbody> 
</table> 

這裏是我的自定義的驗證:

jQuery.validator.addMethod('required_row', function(val, el) { 
    var rowCount = $("#editorRows tbody").length; 
    if (typeof rowCount != "undefined") { 
     return (rowCount > 0); 
    } else { 
     return false; 
    }; 
}, "Your request must contain at least one day."); 

// Validate the form before submitting 
$("form").validate({ 
    rules: { 
     "#editorRow": { required_row: true } 
    } 
}); 
+0

愚蠢的問題,但你有沒有確保沒有JavaScript錯誤。我假設你是使用Firebug,但只是爲了確保。這通常是罪魁禍首。 – 2011-03-31 14:12:43

+0

@Jack不是我所看到的。 – 2011-03-31 14:44:53

+0

好的,我在這個主題上找到了一個[重複發表](http://stackoverflow.com/q/3069871/2535),並且我添加了[功能請求](https://github.com/jzaefferer/jquery-驗證/問題/ 79)。 – 2011-04-11 17:25:59

回答

0

我已經想通了,這是沒有得到觸發,因爲我沒有足夠的驗證器在輸入字段上。我現在有它的工作,但是,有沒有一種方法來添加驗證,而不將其附加到輸入字段?

+1

嘗試使用隱藏的輸入代替 – h3n 2012-08-09 07:40:38

+1

你有代碼示例嗎? – 2016-11-16 15:06:06