我对 jQuery 验证http://validation.bassistance.de/相当陌生,并且正在尝试使用 addMethod 创建自定义事件。我已经编写了看起来正确但显然不是的代码,因为如果事件是真的,那么#email 应该被隐藏而没有任何消息。
发生的情况是,如果事件是真或假,则消息“请输入一个值进行测试!” 总是被解雇。我显然犯了一个史诗般的错误,但似乎找不到纠正它的方法。如果有人能指出我的错误,我将不胜感激。我只发布了相关的代码。谢谢
小提琴代码:http: //jsfiddle.net/DQjTc/
更新:只是一个更新。我注意到,如果在验证后按下提交按钮,则会输入正确的消息而不会输入错误的消息。这就是我需要的。我们从这里去哪里。
add方法代码
$(function() {
$.validator.addMethod( 'emailHide', function(value, element)
{
if (element.value === '')
{
return false;
}
//return true;
$('#email').hide();
$('#emaillbl').html('Email address approved');
},'Please enter a value for testing!'
);
});
验证规则代码
$(function () {
$.validator.setDefaults(
{
errorClass: 'form_error_frmreport',
errorElement: 'div'
});
$("#frmreport").validate(
{
rules:
{
email:
{
//required: true,
email: true,
emailHide: true
},
position:
{
required: true
},
feedback:
{
required: true
}
},
messages:
{
email:
{
//required: "<br />* required: You must enter a valid email address"
},
position:
{
required: "<br />* required: Please state your position"
},
feedback:
{
required: "<br />* required: Please enter as much information regarding the exact nature of the problem"
}
},
submitHandler: function() {
if ($("#frmreport").valid() === true) {
var data = $("#frmreport").serialize();
$.post('/sample/admin/frm10010.php', data, function(msg) {
var messageOutput = '';
for (var i = 0; i<msg.length; i++){
messageOutput += msg[i].box+' ';
}
$("#confirm_department").hide();
var $dialog = $('<div id="dialog"></div>')
.html('Your report was successfully submitted and a representative will respond to you shortly.<br /><br />Thank you.');
$dialog.dialog({
autoOpen: true,
modal: true,
title: 'Report submission successfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function() {
$( this ).dialog( "close" );
}
}
});
$("#frmreport").get(0).reset();
}, 'json');
} else
{
return;
}
},
success: function() {
//validator.resetForm();
//$.html("You have entered a box");
//$("#BA_boxform").get(0).reset();
}
});
});