[英]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.