16

チェックボックスがあり、それに続くテキストは次のようになります。
[チェックボックス]同意します

送信時にチェックボックスがクリックされていない場合、現在のエラーメッセージの表示方法は次のとおりです(errorElement: "div"を使用)。

[チェックボックス]
このフィールドは必須です。
同意します**

私はむしろ欲しいです。
[チェックボックス]同意する
このフィールドは必須です

これを行う方法はありますか?

私が持っている関連するチェックボックスとテキスト要素のhtmlラップは次のようになります。
[div][チェックボックス]同意します[/div][div] [/ div]

私はerrorPlacmentを試しました:その要素だけにそれを適用することを期待して次のように;

...            
messages: {
    usage_terms: {
        required: "Must agree to Terms of Use.",
    //errorElement: "div"
    errorPlacement: function(error, element) {
        error.appendTo( element.parent("div").next("div") );
        }
    }
}
...


うまくいきませんでした。何か案が?。

4

2 に答える 2

36

errorPlacement(オプションとして)メッセージの下にあるのではなく、グローバルオプションであるため、代わりに次のようになります。

messages: {
  usage_terms: "Must agree to Terms of Use."
}
errorPlacement: function(error, element) {
  if(element.attr("name") == "usage_terms") {
    error.appendTo( element.parent("div").next("div") );
  } else {
    error.insertAfter(element);
  }
}

ここでは、要素名をチェックしてどこに行くかを決定していますが、別のチェックを行うこともできます。たとえば、このように動作するすべての要素にクラスを与えて、を実行しelement.hasClass("placeAfter")ます。

于 2010-10-27T12:03:20.467 に答える
0

ファイルhtml

<label for="Q008" class="control-label titular">Question text 008 by example</small></label>
<input type="text" class="form-control" name="Q008" id="Q008" maxlength="500" />
<div id="Q008error"></div>

<label class="control-label titular">Question text 009 by example</label>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_1" value="1" />
    <label for="Q009_1">Text 91</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_2" value="2" />
    <label for="Q009_2">Text 92</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_3" value="3" />
    <label for="Q009_3">Text 93</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_4" value="4" />
    <label for="Q009_4">Text 94</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_5" value="5" />
    <label for="Q009_5">Text 95</label>
</div>
<div class="controls">
    <input type="checkbox" name="Q009" id="Q009_6" value="6" />
    <label for="Q009_6">Text 96</label>
</div>
<div id="Q009error"></div>

jquery検証ファイル

errorPlacement: function(error, element) {
    var elementForm = "", containerError = "";
    offset = element.offset();
    elementForm = element.attr("name");
    containerError = "#" + elementForm + "error";
    error.prependTo(containerError);
    error.addClass('message');
}
于 2018-01-10T14:34:07.573 に答える