jQuery plugin's Validation's方法Valid()
在空集合上調用時會拋出錯誤this[0] is undefined
。我認爲jQuery約定不會對空集合做任何事情,當然不會拋出任何錯誤,或者可能是我錯過了某些東西。請問有人能說明一下嗎?從插件jQuery驗證插件的方法在調用空集合時拋出錯誤
代碼片段。這個[0] - 如下所示 - 在集合爲空時拋出錯誤。
// http://docs.jquery.com/Plugins/Validation/valid
valid: function() {
if ($(this[0]).is('form')) {
return this.validate().form();
} else {
var valid = true;
var validator = $(this[0].form).validate(); // This is the line that throws error
this.each(function() {
valid &= validator.element(this);
});
return valid;
}
所以不這樣做? – mcgrailm 2011-05-05 20:22:32
我們應該在調用方法之前檢查集合嗎?我認爲我們不需要。我更新了代碼以包含addClass的調用,即使在空集合上調用時也不會引發任何錯誤。這是預期的行爲? – 2011-05-05 20:36:10