2015-11-18 67 views
0

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> 
     &nbsp;&nbsp; 
    <h4 id='pleasewait'>Please wait a few seconds. Uploading and spliting your image into different sizes</h4> 


</body> 
</html> 
+1

移動功能'ready'之外,使其_public_。現在它是專用的回調。 – Tushar

+0

[爲什麼我不能在jQuery的document.ready()中定義函數?](http://stackoverflow.com/questions/1055767/why-can-i-not-define-functions-in-jquerys-document已經) –

回答

1

嘗試

<script type="text/javascript"> 
    var file; 
    var name; 
    var size; 
    var type; 
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(); 
    } 
} 

$(document).ready(function(){ 



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(); 



$(':file').change(function(){ 
    file = this.files[0]; 
    name = file.name; 
    size = file.size; 
    type = file.type; 
}); 

}); 

</script> 
+0

謝謝,但是,我試過了,我得到了。 Uncaught ReferenceError:類型未定義 – whoopididoo

+0

我編輯過,請參閱將變量置於文檔準備好之外 var name; var size; var type; –

+0

這樣做。非常感謝。當someomne指出它時也很容易:) – whoopididoo

2

你的功能是$(document).ready()回調的範圍內定義的,不能從外部看到。要麼只在內部定義除$(document).ready()範圍之外的功能。

把你ValidateFile()出來的document.ready的一側將工作

+0

我試過了,我得到了。未捕獲ReferenceError:類型未定義 – whoopididoo

相關問題