【问题标题】:why can't this javascript code read the csv file为什么这个 javascript 代码不能读取 csv 文件
【发布时间】:2014-12-04 09:09:13
【问题描述】:

我是 javascript 编程的新手,我阅读了面向开发人员的专业 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 代码”时,我无法理解他所说的“文件字段被呈现”的含义。尽管如此,我尝试了它并没有奏效。

现在根据我对文件读取工作原理的理解,以下代码应该可以工作,但它不能工作,我不知道为什么。我希望我不会很无聊。

<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>

【问题讨论】:

    标签: javascript csv


    【解决方案1】:

    因为input 元素没有submit 事件。 form 元素可以。如果您希望在按下按钮时调用您的代码,请使用click 事件。

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

    【讨论】:

    • 非常感谢。你让我开心
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多