Jquery的onchange事件不能在php中上传文件


jquery onchange event not working for file uploading in php

我使用jquery ajax方法从php使用文件上传。我已经实现了文件上传使用提交表单时点击一个按钮,但它不工作时输入字段的变化onchange事件。这是我使用的代码,

<div id="photo">
<img id="image" src="<?php echo $uploadedfile;?>" width=100% height=100%>
</div>
<form id="myform" method="post" enctype="multipart/form-data">
<div id="pick">Change Photo
<input type="file" name="files" id="upload">
</div>
<input type="submit" value="send" name="submit">
</form>
jquery使用

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$("form#myform").submit(function(event){
event.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
url : "main.php",
type: 'POST',
datatype : "JSON",
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returndata) {
    $("#image").attr({
        'src' : returndata.uploadedfile
});
}
}); 
return false;
});
</script>

php代码

<?php   
$uploadedfile = "default.jpg";
if(isset($_POST['submit'])){
if(isset($_FILES["files"]["name"])){

$filename = $_FILES["files"]["name"];   
$tmppath  = $_FILES["files"]["tmp_name"];
$ext = pathinfo($filename,PATHINFO_EXTENSION);
$targetfile = "uploads/".basename($filename);
if($_FILES["files"]["size"] > 0)
{
if($ext == "jpg")
{
if(file_exists($targetfile)){
$uploadok = false;
$error = "file already exists";
}
else {
$uploadok = true;
}
}
else
{
$uploadok = false;
$error = "only jpg format is allowed";
}   
}
else {
$uploadok = false;
$error = "filesize is too short";   
}
if($uploadok == true){  
move_uploaded_file($tmppath,$targetfile);
$uploadedfile = $targetfile;
$error = "no errors!";
}else{
$uploadedfile = "default.jpg";
}
header("Content-type: text/x-json");
$data = array('error'=>$error, 'uploadedfile'=>$uploadedfile);
echo json_encode($data);
exit();
}
}
?>

我想在改变上实现这一点,而不是通过点击一个按钮。你知道吗??

修改如下:

$("form#myform").submit(function (event) 

:

$("form#myform").change(function (event)