2014-12-04 61 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

相关问题