简体   繁体   English

未捕获ReferenceError:未定义onsubmit函数

[英]Uncaught ReferenceError: onsubmit function is not defined

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 我有一个需要移出document.ready函数的函数和变量

This is my error; 这是我的错误;

Uncaught ReferenceError: ValidateFile is not defined I have looked at other solutions for this problem but none is working for me. 未被发现的ReferenceError:未定义ValidateFile我已经查看了其他解决方案来解决此问题,但没有一个对我有用。

If I move the function ValidateFile() to the head section then I get an error that type is not defined. 如果我将函数ValidateFile()移到头部,则会收到一个错误,指出该类型未定义。

What am I missing please? 我想念什么? Here is my code: 这是我的代码:

<!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>

Your function is defined within the scope of the $(document).ready() callback and cannot be seen from the outside. 您的函数在$(document).ready()回调范围内定义,无法从外部看到。 Either define the function outside the $(document).ready() scope of call it only from within. 只能在内部的$(document).ready()范围之外定义函数。

put your ValidateFile() out side of document.ready will work 将您的ValidateFile()放在document.ready的外面

try 尝试

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM