2014-12-04 46 views
0

我在javascript編程中很新的B,我讀了開發人員專業的JavaScript書第3版。此外,我嘗試了很多選項,從網站How can you read a file line by line in JavaScript?或此:How can you read a file line by line in JavaScript?爲什麼不能這個javascript代碼讀取csv文件

對於第二個選項,我在說「在呈現文件字段後請記住放置您的JavaScript代碼」時,理解他的意思是「文件字段已呈現」的問題。儘管如此,我試了一下,但沒有奏效。

現在從我理解文件閱讀的工作原理,下面的代碼應該可以工作,但它不會,我不知道爲什麼。我希望我不是很無聊。

<p id="output"> 
</p> 
<form id="form1" action=""> 
    <input type="file" id="input"> 
    <input type="button" id="readFil" value="read" onsubmit="readFile()"> 
</form> 
<script> 
    function readFile() { 
    var selected_file = document.getElementById('input').files[0]; 
    reader = new FileReader(); 
    reader.readAsText(selected_file); 
    reader.onload = function() { 
     document.getElementById("output").innerHTML = reader.result; 
    }; 
    } 
</script> 

回答

1

因爲input元素沒有submit事件。 form元素。如果您希望在按下按鈕時調用您的代碼,請使用click事件。

<input type="button" id="readFil" value="read" onclick="readFile()"> 
<!-- Change is here -----------------------------^^^^^   --> 
+0

非常感謝。你讓我今天一整天都感覺很好 – zeroday 2014-12-04 09:16:31

相關問題