简体   繁体   English

此javascript代码在没有for循环的情况下可以正常运行,但是没有循环。任何帮助,我都尝试了一切

[英]This javascript code is running properly without the for loop but not with it.Any help, I have tried everything

Firstly, I have inputted this file and separate all the lines be split() function.Then, I have saved the location( var lat and lng ) and soil type ,but the problem is if I do not use 'for' loop and check whether my code without this loop is working or not , then it is working. 首先,我输入了这个文件并分隔了所有行的split()函数。然后,我保存了位置(var lat和lng)和土壤类型,但是问题是我是否不使用'for'循环并检查无论我的没有此循环的代码是否正常运行,都可以正常工作。 This is the link to the text file which i want to read : 这是我要阅读的文本文件的链接:

https://drive.google.com/file/d/0B_NBRSiUy-R1ZmhwOVhCZFQ4cWc/view?usp=sharing https://drive.google.com/file/d/0B_NBRSiUy-R1ZmhwOVhCZFQ4cWc/view?usp=sharing

Please Help. 请帮忙。

<html>
<head>
</head>

<body>
<input type="file" id="fileinput" />
<script type="text/javascript">
  function readSingleFile(evt) {
    var f = evt.target.files[0]; 

    if (f) {
      var r = new FileReader();
      r.onload = function(e) { 
          var contents = e.target.result;


        var lines = contents.split('\n');
        var lng = new Array();
        var lat = new Array();
        var soil = new Array();
        var splitted = new Array();
        var k;

        for(var i=0;i<lines.length;i++){
        splitted = lines[i].split(" ");
        lng[i] = splitted[0];
        lat[i] = splitted[1];
        k = splitted.length - 5;
        document.write("First Step Done");
        switch(k){
        case 1: 
                soil[i]= splitted[5]; 
                break;
        case 2: 
                soil[i]= splitted[5] + " " + splitted[6]; 
                break;
        case 3: 
                soil[i]= splitted[5] + " " + splitted[6] + " " + splitted[7]; 
                break;
        case 4: 
                soil[i]= splitted[5] + " " + splitted[6] + " " + splitted[7] + " " + splitted[8]; 
                break;
        }

      }
      document.write("done");

      r.readAsText(f);
    } else { 
      alert("Failed to load file");
    }
  }

          document.getElementById('fileinput').addEventListener('change',readSingleFile, false);
</script>
</body>
</html>

you are missing a curly bracket: 您缺少大括号:

    document.write("done");
};                              // <-- this is missing
r.readAsText(f);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript/Jquery:代码不重复我尝试过的任何循环 - Javascript/Jquery: code not repeating with any loop I've tried Javascript 运行不正确 - 尝试了一切 - Javascript not Running Correctly - Tried Everything Javascript 分配有问题:数组和 object 迭代但我不知道解决方案是什么,我已经尝试了一切 - Having a problem with a Javascript assignment: Array and object iteration but I have no idea what the solution is, I have tried everything Javascript 分配有问题,我不知道解决方案是什么,我已经尝试了一切 - Having a problem with a Javascript assignment and I have no idea what the solution is, i have tried everything JavaScript代码无法正常运行 - Javascript code not running properly 我试图拖放一个元素。 为什么我的代码不能正常工作? - I have tried to drag and drop an element. Why won't my code work properly? 无法加载JavaScript。 尝试了我能想到的一切 - JavaScript not being loaded. Tried everything I can think of 在javascript中过滤二维数组,我尝试了一切 - filtering a 2D array in javascript, i tried everything 我尝试使用3反应导航版本运行我的drawerNavigation代码,但仍然说错误 - I have tried running my drawerNavigation code with 3 react navigation version but still says error 我想用@keyframes 将 animation 添加到 div 中。 我基本上已经尝试了我所知道的一切 - i want to add an animation to the div with @keyframes. I have tried basically everything i know
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM