2011-04-13 43 views
1

我正在使用jQuery驗證表單。我需要添加一些功能,以便在從選擇/下拉菜單中選擇特定選項時需要額外的字段。嘗試使用jQuery驗證依賴於另一個選項的字段時出現錯誤驗證插件

下面是他們的jQuery的網站需要certrain領域上提供如果一個選項在一個複選框(http://docs.jquery.com/Plugins/Validation/validate#options)檢查例如:

$(".selector").validate({ 
    rules: { 
    contact: { 
     required: true, 
     email: { 
     depends: function(element) { 
      return $("#contactform_email:checked") 
     } 
     } 
    } 
    } 
}) 

我有它的工作,如果選擇一個特定的選項,它會顯示額外的字段。一旦選擇菜單失去焦點,控制檯中會出現以下錯誤:

Uncaught TypeError: Cannot call method 'call' of undefined

我在JSFiddle上發佈了一個示例。如果有人可以看看,我將不勝感激。我需要儘快得到這個工作!

http://jsfiddle.net/2EfPZ/

+0

你有驗證插件包含源代碼? http://jsfiddle.net/2EfPZ/13/ – MacAnthony 2011-04-13 23:24:39

+0

jQuery文檔中顯示的示例目前是錯誤的,因爲它顯示了上面的示例,但工作的代碼是@ Andrew的下面。 – eaglestorm 2013-10-28 06:35:40

回答

4

的錯誤發生,因爲你需要的depends功能與特定的驗證規則關聯:

$form.validate({ 
    rules: { 
     favoriteSport: { 
      required: true 
     }, 
     position: { 
      required: { 
       depends: function(element) { 
        return $('#favoriteSport').val() === '1'; 
       } 
      } 
     }, 
     player: { 
      required: { 
       depends: function(element) { 
        return $("#favoriteSport").val() === '1'; 
       } 
      } 
     } 
    }, 
    messages: { 
     favoriteSport: "You must select a favorite sport", 
     position: "You must select a position because you selected football", 
     player: "You must select a player because you selected football" 
    } 
}); 

工作例如:http://jsfiddle.net/2EfPZ/20/