2012-06-20 43 views
3

我正在使用jQuery驗證並嘗試獲取一對單選按鈕顯示在第一個按鈕之前的錯誤消息。單選按鈕的jQuery驗證自定義錯誤消息顯示

這裏是驗證:

errorPlacement: function(error, element) { 
if (element.attr("type") == "radio") 
    error.insertBefore(element.first()); 
else 
    error.insertAfter(element); 
} 

下面是HTML:

<div class="full"> 
<label id="sample">Have you received your sample pack?</label> 

<input type="radio" name="sample" id="sample_yes" value="yes" /> 
<label for="eliteflexsample_yes">Yes</label> 

<input type="radio" name="sample" id="sample_no" value="no" /> 
<label for="eliteflexsample_no">No</label>  
</div> 

眼下錯誤顯示第一個單選按鈕後了。

+0

嘗試登錄類型的值到控制檯。 在if之前加上這一行: console.log(element.attr(「type」)); – bilalq

回答

4

這是工作:

$(document).ready(function() { 

    $('form').validate({ 

     // your validation options, 

     errorPlacement: function(error, element) { 
      if (element.attr("type") == "radio") { 
       error.insertBefore(element); 
      } else { 
       error.insertAfter(element); 
      } 
     } 

    }); 

});​ 

Working Demo:

http://jsfiddle.net/DR5Aw/2/

相關問題