我正在尝试创建一个带有两个按钮(保存和提交)的简单申请表。我正在使用 jQuery 验证插件。为了提交表格,申请人必须正确填写整个表格。没有空框,每个框中都有有效数据。但是,为了保存,他们可以将任意数量的字段留空,但他们填写的字段必须是有效的。(即数字框中没有字母)。
为此,我有两个单独的验证函数,根据用户单击的按钮,调用两者之一。这适用于提交按钮,并且第一次适用于保存按钮。但是,如果用户在一个用于数字的框中输入了一些乱码并点击了两次保存,则表单仍然会提交。他们第一次点击保存时,它的行为正确。
对于我的生活,我无法弄清楚为什么。我的jQuery代码如下:
$(document).ready(function(){
$("#csa_submit_button").live("click",function(){
//alert("submit");
$("#csa_submit_flag").val("true");
validator = $("#csa_form").validate({
"rules": {
"csa_phone" : {required: true, digits: true, minlength: 10, maxlength: 10},
"csa_current_grants" : {number: true, required: true},
"csa_current_work_study" : {number: true, required: true},
"csa_balance_no_loans" : {number: true, required: true},
"csa_current_loans" : {number: true, required: true},
"csa_textbooks" : {number: true, required: true},
"csa_total_to_date" : {number: true, required: true},
"csa_parent_plus" : "required",
"csa_student_loans" : "required",
"csa_balance" : {number: true, required: true},
"csa_employment_contributions" : {number: true, required: true},
"csa_current_financial_situation" : "required",
"csa_activities" : "required",
"csa_request_amount" : {required: true, number: true},
"csa_term": {required: true, minlength: 1}
},
"messages": {
"csa_term": "You must select at least one term",
"csa_phone": "You must enter a phone number in the following format: ##########"
},
errorPlacement: function(error, element) {
if (element.attr('type') === 'radio') {
error.insertAfter(element.parent());
}
else if (element.attr('name') === 'csa_employment_contributions'){
error.insertAfter(element.parent());
}
else if (element.attr('name') === 'csa_term') {
error.insertAfter(element.parent());
}
else {
error.insertAfter(element);
}
}
});
});
$("#csa_save_button").live("click",function(){
$("#csa_submit_flag").val("false");
validator = $("#csa_form").validate({
"rules": {
"csa_phone" : {number: true},
"csa_current_grants" : {number: true},
"csa_current_work_study" : {number: true},
"csa_balance_no_loans" : {number: true},
"csa_current_loans" : {number: true},
"csa_textbooks" : {number: true},
"csa_total_to_date" : {number: true},
"csa_balance" : {number: true},
"csa_employment_contributions" : {number: true},
"csa_request_amount" : {number: true}
},
"messages": {
"csa_phone": "You must enter a phone number in the following format: ##########"
},
errorPlacement: function(error, element) {
if (element.attr('name') === 'csa_employment_contributions'){
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
});
});