2011-06-30 23 views
0

jQuery検証プラグインによって生成されたエラーメッセージを特定の場所に表示したいとします。どうやってやるの?たとえば、私は2つのラジオボタンを持っていますが、デフォルトでは、最初のラジオボタンの隣にエラーメッセージが表示されます(テキスト "1年前"の前に表示されます。私はそれを行うjQuery検証エラーメッセージの問題

HTML:?。

<div > 
    <label for="period">Select period</label> 
    <div class = "radio-buttons"> 
     <input type="radio" name="period" > 1 year 
     <input type="radio" name="period" > 2 years 
     <span class="error">Display error message here.</span> 
    </div>       
</div> 

は、私がスパンでエラーメッセージを表示

$("#test").validate({ 
      errorElement: "span", 
    }); 

答えて

2

この

を試してみて、他のスパン内のスパンを避けるために、DIVにエラー・コンテナを変更します。

<div class = "radio-buttons"> 
    <input type="radio" name="period" > 1 year 
    <input type="radio" name="period" > 2 years 
    <div class="error"></div> 
</div> 
1

は、あなたのコード内で<div id="expiryyear">などのような適切な要素を持っていることを確認してください

errorPlacement: function(error, element) { 
    if (element.attr("id") == "expirymonth" || element.attr("id") == "expiryyear") 
      error.insertAfter("#expiryyear"); 
    else if (element.attr("id") == "cvv") 
      error.insertAfter("#cvvError"); 
    else if (element.attr("id") == "opt_havecreditterms" || element.attr("id") == "opt_applyforcreditterms") 
      error.insertAfter("#credit-option-error"); 
    else if (element.attr("id") == "bankstate" || element.attr("id") == "bankzip") 
      error.insertAfter("#bankzip"); 
    else 
      error.insertAfter(element); 

}