我一直遇到这么多麻烦,但我无法理解。我有一个表单,我用jQuery验证它。一切都工作甚至文件的类型== jpg,png但我检查是否有任何上传的验证不起作用。如果没有选择文件,我需要它停止。
//begins check
$("#story").submit(function (e) {
if ($("#human-story").val() == '8') {
var fd = new FormData();
var fileObj = document.getElementById('fileUpload').files[0];
var filename = fileObj.name;
var size = fileObj.size;
fd.append('date', (new Date()).toString());
fd.append('input', fileObj);
var t = filename.substring(filename.length-3, filename.length).toLowerCase();
var imgVal = $('#fileUpload').val(); //this is to grab the input type file value
if(['png', 'jpg' ,'gif', 'jpeg'].indexOf(t) === -1){
alert("Incorrect File -- Please upload an image");
e.preventDefault();
return;
};
if(imgVal==''){
alert("empty input file");
e.preventDefault();
}//this part wont work ^^^ to see if no file was chosen
alert("your story has been submitted. Thank you.");
} else {
alert("wrong answer");
e.preventDefault();
};
});//end submit function
HTML:
<input type="file" name="uploaded_file" id="fileUpload">
<input type="text" class="form-control" id="human-story" name="human-story" placeholder="Your Answer" required="">