2012-07-25 103 views
0
<?php 
    if (!empty($_FILES)){ 
     echo "<pre>"; 
     echo $_FILES['file']['type']; 
     echo "</pre>"; 
     return; 
    } 
?> 
<script type="text/javascript"> 
    function autoUpLoad(){ 
     $("#txtHint").html("<center><img src='include/images/loader.gif' />Reading selected file...</center>"); 
     $(document).ready(function(){ 
      $("#file").change(function(){ 
       $("#myForm").submit(function(data){ 
        $("#txtHint").html(data); 
       }); 
      }); 
     }); 
    } 
</script> 
<form id="myForm" method="post" enctype="multipart/form-data"> 
    <input type="file" name="file" id = "file" onchange='autoUpLoad()'/> 
    <input type="submit" value="Send" /> 
</form> 
<div id="txtHint"> 
    test 
</div> 

上述代碼無法正常工作,我不確定這裏出現了什麼問題?它的工作原理只有當我刪除這些行:

function(data){ 
    $("#txtHint").html(data); 
} 

它只是不允許我將數據返回給txtHint。任何人都可以向我解釋如何使它工作?

+0

我不明白這個標籤。 ** <? php ** – chhameed 2012-07-25 06:14:31

+0

Hameed是jquery返回過程數據。 – 2012-07-25 06:22:04

回答

2

您正在綁定提交事件,而不是觸發它。

.submit()帶回調參數的方法用於綁定提交事件,不帶參數來觸發提交事件。

你應該這樣做如下:

$(document).ready(function() { 
    $("#myForm").submit(function (data) { 
     $("#txtHint").html(data); 
    }); 
    $("#file").change(function() { 
     $("#myForm").submit(); 
    }); 
}); 
+0

但結果不會出現在div txtHint中。一切看起來像重新加載 – 2012-07-25 06:21:27

+1

@JonathanChan'.submit'只需激發提交事件,頁面將被重新加載,這不是ajax。 – xdazz 2012-07-25 06:25:32

+0

哦!有沒有任何Ajax函數可以解決我的問題?我真的很難學習ajax jquery ... – 2012-07-25 06:31:36

相關問題