好吧,我使用jQuery驗證插件來檢查簡單表單上的錯誤。除錯誤位置外,一切正常。我試圖讓我的錯誤消息放置在包含錯誤的INPUT後面的SPAN元素中。出於某種原因,下面的代碼將錯誤消息放在似乎是一個全新的LABEL元素的位置。任何想法我在這裏做錯了嗎?jQuery驗證錯誤信息放置
我的HTML
<form action="" id="contact" method="POST">
<p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" class="text proper"/><span class="errMsg"></span>
</p>
<p>
<label for="email">* Email:</label>
<input type="text" name="email" id="email" class="required email text"/><span class="errMsg"></span>
</p>
<p>
<label for="extra">*Question:</label>
<textarea name="onlyText" id="onlyText" class=""></textarea><span class="errMsg"></span>
</p>
<button type="submit" id="send">Send Now</button>
<div id="results"></div>
</form>
我的jQuery
<script>
jQuery(document).ready(function($){
$.validator.addMethod("regex",function(value, element, regexp) {
var check = false;
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"No special Characters allowed here. Use only upper and lowercase letters (A through Z; a through z), numbers and punctuation marks (. , ; ? ' ' \" - ~ ! @ $ %^& * ( ) _ +/< > { })"
);
$("#contact").validate({
rules: {
onlyText:{
required: true,
maxlength: 8000,
regex: /^[0-9A-Za-z\s`[email protected]$%^&*()+{}|;'",.<>\/?\\-]+$/
},
name:{
required: false,
maxlength: 75,
regex: /^[0-9A-Za-z\s`[email protected]$%^&*()+{}|;'",.<>\/?\\-]+$/
}
} ,
errorPlacement: function(error, element) {
error.appendTo(element.next()); //* THIS IS WHERE I AM HAVING TROUBLE WITH
});
});
</script>
你在你的JavaScript中的一些錯誤,所以我在這裏嘗試一些清理:HTTP ://jsfiddle.net/XNQVr/ – Robb