OK So its kind of the same but not entirely. I had a function and variables that needed to be moved out of the document.ready function未捕獲ReferenceError:onsubmit函數未定義
這是我的錯誤;
Uncaught ReferenceError: ValidateFile is not defined I have looked at other solutions for this problem but none is working for me.
如果我將函數ValidateFile()移動到頭部分,那麼我得到一個類型未定義的錯誤。
我錯過了什麼? 這裏是我的代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Choose a file to upload</title>
</head>
<body>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2-bootstrap-css/1.4.6/select2-bootstrap.min.css">
<script type="text/javascript">
$(document).ready(function(){
var file;
var name;
var size;
var type;
window.addEventListener('message',function(event) {
//if(event.origin !== 'http://demo.dev) return;
alert('message received: ' + event.data,event);
event.source.postMessage('hola back !',event.origin);
},false);
$('progress').hide();
$('#pleasewait').hide();
function ValidateFile(){
var formData = new FormData($('form')[0]);
var str=type.substring(0,5);
if (str!='image'){
alert('the file '+ name +' is not a valid image, Please upload only image files, thank you. The management!');
}else{
//alert(' I got the file : ' + formData + ' file: ' +file+' name: '+name+' size: '+size+' type: '+type);
$('progress').show();
$('#pleasewait').show();
}
}
$(':file').change(function(){
file = this.files[0];
name = file.name;
size = file.size;
type = file.type;
});
});
</script>
<p><h1>Choose a file to upload</h1></p>
<form name="UploadForm" enctype="multipart/form-data" class="form" action="/api/v2/process_images" method="post" onsubmit="javascript: return ValidateFile();" >
File: <input type="file" name="file1" />
<input type="submit" value="upload" id="upload_image"/>
</form>
<progress ></progress>
<h4 id='pleasewait'>Please wait a few seconds. Uploading and spliting your image into different sizes</h4>
</body>
</html>
移動功能'ready'之外,使其_public_。現在它是專用的回調。 – Tushar
[爲什麼我不能在jQuery的document.ready()中定義函數?](http://stackoverflow.com/questions/1055767/why-can-i-not-define-functions-in-jquerys-document已經) –