2010-03-13 49 views
1

我想使用jQuery驗證突出顯示我的單選按鈕的標籤,而不是我的其他輸入的標籤。我有一個名爲'type'的單選按鈕組的標籤。我似乎無法讓它工作!jQuery驗證 - 突出顯示無線標籤

$(document).ready(function(){ 
    $("#healthForm").validate({ 

highlight: function(element, errorClass) { 
    $(element).addClass(errorClass) 
    $(element.form).find("label[for='type']") 
        .addClass("radioerror"); 
    }, 
    unhighlight: function(element, errorClass) { 
    $(element).removeClass(errorClass) 
    $(element.form).find("label[for='type']") 
        .removeClass("radioerror"); 
    }, 
    errorPlacement: function(error, element) { 
     } 
    }); 
    }); 
+0

什麼不工作,類分配或類樣式本身? – 2010-03-13 03:51:02

+0

班級分配。我認爲我的問題是用find(「label [for ='type']」) - 這是調用它的正確方法嗎? – Michael 2010-03-13 03:52:09

+0

那裏有多種形式?在你的HTML中,'type'是分配給'ID'還是'Name'? – 2010-03-13 04:01:14

回答

1

我真的不熟悉jQuery的驗證插件,但這個看起來不正確:

$(element.form).find("label[for='type']") 

首先,我認爲$(element.form)應該是$(element).closest('form')?我可能是錯的:P

其次,.find("label[for='type']")與「element」無關,因爲同一表單中的其他元素將獲取相同的標籤。

如果標籤是單選按鈕的兄弟,您可能希望直接使用sibling()

0

我將您的代碼更改爲以下內容,然後運行!

$(document).ready(function(){ 
    $("#healthForm").validate({ 
     highlight: function(element, errorClass) { 

      if(element.type == 'radio') { 
       $(element.form).find('[name="' + element.name + '"').each(function(){ 
        var $this = $(this); 
        $this.closest('label[for="' + $this.attr('id') + '"').addClass(errorClass); 
       }); 
      } 

     }, 
     unhighlight: function(element, errorClass) { 

      if(element.type == 'radio') { 
       $(element.form).find('[name="' + element.name + '"').each(function(){ 
        var $this = $(this); 
        $this.closest('label[for="' + $this.attr('id') + '"').removeClass(errorClass); 
       }); 
      } 

     }, 
     errorPlacement: function(error, element) { 
     } 
    }); 
});