2013-02-16 70 views
0

我有以下代碼來上傳照片。我目前有這個過程集,所以點擊提交按鈕就可以上傳。如何在不提交按鈕的情況下自動上傳該代碼?由於腳本上傳文件時未提交

HTML:

<form data-ajax="false" id="imageform" action="imageup.php" method="POST" enctype="multipart/form-data" name="form"> 
    <a id="companylink" href=""> 
     <div id="yourBtn" onclick="getFile()" onchange="readURL(this);"> 
      <img id="companyprofileimg" src="image/<?php echo $row['photo']?>"/> 
     </div> 
     <span id="cetext"><?php echo $row['username']?></span> 
    </a> 
    <div style='height: 0px;width: 0px; overflow:hidden;'> 
     <input id="upfile" type="file" value="upload" name="file" onchange="readURL(this);"/> 
    </div> 
    <a data-theme="g" data-ajax="false" data-role="button" id="imagesub" onclick="$('#imageform').submit();" aria-disabled="false">Upload Image</a> 
</form> 

JAVASCRIPT:

function getFile(){ 
    document.getElementById("upfile").click(); 
} 
function sub(obj){ 
    var file = obj.value; 
    var fileName = file.split("\\"); 
    document.getElementById("companyprofileimg").innerHTML = fileName[fileName.length-1]; 
    document.myForm.submit(); 
    event.preventDefault(); 
    document.getElementById('imageform').submit(); 
    } 

回答

1

試試這個

function readURL(input) { 
    if (input.files && input.files[0]) { 
     var reader = new FileReader(); 

     reader.onload = function (e) { 
      $('#companyprofileimg') 
       .attr('src', e.target.result) 
       .width(50) 
       .height(50) 
     }; 

     reader.readAsDataURL(input.files[0]); 
     $('#imageForm')[0].submit(); 
    } 
} 
+0

我編輯了我的代碼。你能給我一個關於新代碼的解決方案嗎?我擺脫了readURL功能,這是不需要的。謝謝。 – Steven 2013-02-16 16:46:33

+0

@Steven:另請檢查此[鏈接](http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload)和[鏈接](http://stackoverflow.com/questions/166221/how- can-i-upload-files-asynchronously-with-jquery)如果你想要一個像AJAX一樣的體驗 – user1428716 2013-02-16 16:52:19

+0

@ user1428716看起來不錯,但是這兩者仍然需要點擊一個提交按鈕。 – Steven 2013-02-16 16:59:20

1

的JavaScript:

document.getElementById('imageForm').submit();

+0

這裏我想補充一點? – Steven 2013-02-16 16:32:21

0

我想通了,我剛添加的電平變化提交給輸入,感謝您的幫助鄉親