我正在使用jquery验证插件并使用此代码,我尝试在字段不符合要求的规范时触发一些错误
$(document).on('click', '#btn_save', function() {
$('form#new_inquiry').validate({
onkeyup: false,
errorClass: 'error',
validClass: 'valid',
highlight: function(element) {
$(element).closest('div').addClass("f_error");
},
unhighlight: function(element) {
$(element).closest('div').removeClass("f_error");
},
errorPlacement: function(error, element) {
$(element).closest('div').append(error);
},
rules: {
pname: { required: true },
pid: { required: true, min: 1 },
country: { required: true },
cid: { required: true, min: 1 },
city: { required: true },
delivery_date: {
require_from_group: [1, '.delivery']
},
delivery_text: {
require_from_group: [1, '.delivery']
},
address: { required: true },
content: { required: true }
},
messages: {
pname: "You must enter a customer name before saving",
country: "You must select a valid country",
city: "Please fill in the city",
address: "Please fill in the address",
content: "Please fill in the order content"
},
invalidHandler: function(form, validator) {
$.sticky("Inquiry cannot be saved for the moment. </br>Please corect errors marked up", {autoclose : 5000, position: "top-right", type: "st-error" });
},
submitHandler: function(form) {
$.ajax({
url: 'view/inquiry/inquiry_insert-exec.php',
type: 'post',
dataType: 'json',
data: $('form#new_inquiry').serialize(),
beforeSend: function() {
$('#amount').val($('#amount').val().toString().replace(/\,/g, '.'));
$('#btn_save').attr('disabled', true);
$('#btn_save').after('<span class="wait"> <img src="<?= DIR_MEDIA;?>img/ajax_loader.gif" alt="" /></span>');
},
complete: function() {
$('#btn_save').attr('disabled', false);
$('.wait').remove();
},
success: function(json) {
if (json['status']) {
if (json['id']) {
location = '?route=home/inquiry/insert&id='+json['id']+"&tab=#tab2";
//$('#myTab a[href="#tab2"]').tab('show');
} else {
location = '?route=home/inquiry';
}
} else {
$.sticky("There were errors while saving inquiry.</br>" + json['status'], {autoclose : 5000, position: "top-right", type: "st-error" });
}
}
});
}
});
if ($('#new_inquiry').valid()) {
}
});
但是,这段代码什么也没做。我想我在某个地方有错误,但我不明白它可能在哪里 我正在使用Firebug来跟踪错误,但在单击#btn_save后仍然没有错误触发 在单击两个按钮时检查validate(),并且根据单击的按钮,下一个事件将是不同的,因此在click事件中使用valid()函数,但仍然没有任何反应。我怎么能看到错误?我想如果代码什么都不做
答案 0 :(得分:2)
您无需在点击事件中添加它。这是代码。
$(document).ready(function() {
$('#add_branch_form').validate({
onkeyup: false,
errorClass: 'error',
validClass: 'valid',
highlight: function(element) {
$(element).closest('div').addClass("f_error");
},
unhighlight: function(element) {
$(element).closest('div').removeClass("f_error");
},
errorPlacement: function(error, element) {
$(element).closest('div').append(error);
},
rules: {
pname: {required: true},
pid: {required: true, min: 1},
country: {required: true},
cid: {required: true, min: 1},
city: {required: true},
delivery_date: {
require_from_group: [1, '.delivery']
},
delivery_text: {
require_from_group: [1, '.delivery']
},
address: {required: true},
content: {required: true}
},
messages: {
pname: "You must enter a customer name before saving",
country: "You must select a valid country",
city: "Please fill in the city",
address: "Please fill in the address",
content: "Please fill in the order content"
},
invalidHandler: function(form, validator) {
$.sticky("Inquiry cannot be saved for the moment. </br>Please corect errors marked up", {autoclose: 5000, position: "top-right", type: "st-error"});
},
submitHandler: function(form) {
if ($(form).valid()) {
$.ajax({
url: 'view/inquiry/inquiry_insert-exec.php',
type: 'post',
dataType: 'json',
data: $('form#new_inquiry').serialize(),
beforeSend: function() {
$('#amount').val($('#amount').val().toString().replace(/\,/g, '.'));
$('#btn_save').attr('disabled', true);
$('#btn_save').after('<span class="wait"> <img src="<?= DIR_MEDIA;?>img/ajax_loader.gif" alt="" /></span>');
},
complete: function() {
$('#btn_save').attr('disabled', false);
$('.wait').remove();
},
success: function(json) {
if (json['status']) {
if (json['id']) {
location = '?route=home/inquiry/insert&id=' + json['id'] + "&tab=#tab2";
//$('#myTab a[href="#tab2"]').tab('show');
} else {
location = '?route=home/inquiry';
}
} else {
$.sticky("There were errors while saving inquiry.</br>" + json['status'], {autoclose: 5000, position: "top-right", type: "st-error"});
}
}
});
}
}
});
});