我无法在客户端进行表单验证并在此之后提交。问题可能出在代码的提交部分,因为如果删除提交部分,验证部分就可以工作。有人可以帮我吗? 这是我的代码:
var allowed_file_size = "5242880";
var allowed_files = ['image/png', 'image/psd', 'image/jpeg', 'image/eps'];
var border_color = "#036a96"; //initial input border color
$("#contact_body").submit(function(e) {
e.preventDefault(); //prevent default action
proceed = true;
//simple input validation
$($(this).find("input[data-required=true], textarea[data-required=true]")).each(function() {
if (!$.trim($(this).val())) { //if this field is empty
$(this).css('border-color', 'red'); //change border color to red
proceed = false; //set do not proceed flag
}
//check invalid email
var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if ($(this).attr("type") == "email" && !email_reg.test($.trim($(this).val()))) {
$(this).css('border-color', 'red'); //change border color to red
proceed = false; //set do not proceed flag
}
}).on("input", function() { //change border color to original
$(this).css('border-color', border_color);
});
//check file size and type before upload, works in modern browsers
if (window.File && window.FileReader && window.FileList && window.Blob) {
var total_files_size = 0;
$(this.elements['my-file'].files).each(function(i, ifile) {
if (ifile.value !== "") { //continue only if file(s) are selected
if (allowed_files.indexOf(ifile.type) === -1) { //check unsupported file
$("#push").html(ifile.name + " nije dozvoljen tip fajla!");
proceed = false;
}
total_files_size = total_files_size + ifile.size; //add file size to total size
}
});
if (total_files_size > allowed_file_size) {
$("#push").html("Maksimalna velicina fajla je 5 MB!");
proceed = false;
}
}
if (proceed) {
var post_url = $(this).attr("action"); //get form action url
var request_method = $(this).attr("method"); //get form GET/POST method
var form_data = new FormData(this); //Creates new FormData object
$.ajax({ //ajax form submit
url: post_url,
type: request_method,
data: form_data,
dataType: "json",
contentType: false,
cache: false,
processData: false
}).done(function());
}
});
答案 0 :(得分:0)
从您的代码中,$.ajax
函数调用:
}).done(function());
这是不正确的语法。这个功能需要一个身体。它应该是}).done(function() {});
答案 1 :(得分:0)
认为您可能缺少done
回调的函数体,这会导致语法错误。如果您打开了DEV控制台,那就更容易发现了。
}).done(function());
应该是
}).done(function() {} );