for循环中没有识别'i'值(错误行由注释行后跟错误表示) [英] 'i' value is not recognised inside for loop ( error line is indicated by comment line followed by error)

查看:164
本文介绍了for循环中没有识别'i'值(错误行由注释行后跟错误表示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,



i在这里粘贴了我的完整js代码..





Please Help me,

i have pasted my full js code here..


$(function ()
{
    //alert('Im Ready');
    var db = openDatabase('Test', '1.0', 'Test', 2 * 1024 * 1024);
    db.transaction(function (tx)
    {
        tx.executeSql('drop table countries ');
        tx.executeSql('CREATE TABLE IF NOT EXISTS countries (id INTEGER PRIMARY KEY AUTOINCREMENT,countryId,countryname)');
        //alert('table created');
    });
    main();
});

function main() {
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        var url = "http://ws.geonames.org/searchJSON";

        xmlhttp.open("GET", url, true);
        xmlhttp.send();

        xmlhttp.onreadystatechange = function () {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                jsondata = xmlhttp.responseText;
                var json = eval('(' + jsondata + ')');

                pushtoDB(json);
            }
        }
}

function pushtoDB(jsondata) {

    var db = openDatabase('Test', '1.0', 'Test', 2 * 1024 * 1024);
    var i = 0;

     db.transaction(function (tx) {

        for ( i = 0; i <= jsondata.geonames.length; i++) {

var cid = jsondata.geonames[i].countryId; // Error:Cannot read property 'countryId'of undefined)
            var name = jsondata.geonames[i].lng;

            tx.executeSql('INSERT  INTO countries (countryId,countryname) VALUES ("' + cid + '","' + name + '")');
        }
        
    });
}

推荐答案

(function()
{
// alert('Im Ready' );
var db = openDatabase('Test','1.0','Test',2 * 1024 * 1024);
db.transaction(function(tx)
{
tx.executeSql('drop table countries');
tx.executeSql('CREATE TABLE IF NOT NOT EXISTS countries(id INTEGER PRIMARY KEY AUTOINCREMENT,countryId,countryname)');
// alert(' table created');
});
main();
});

函数main(){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}

var url =http://ws.geonames.org/searchJSON;

xmlhttp.open(GET,url,true);
xmlhttp.send();

xmlhttp.onreadystatechange = function(){

if(xmlhttp.readyState == 4&& xmlhttp.status == 200){
jsondata = xmlhttp.responseText;
var json = eval('('+ jsondata +')');

pushtoDB(json);
}
}
}

函数pushtoDB(jsondata){

var db = openDatabase('Test','1.0', '测试',2 * 1024 * 1024);
var i = 0;

db.transaction(function(tx){

for(i = 0; i< = jsondata.geonames.length; i ++){

var cid = jsondata.geonames [i] .countryId; //错误:无法读取未定义的属性'countryId')
var name = jsondata.geonames [i] .lng;

tx.executeSql('INSERT INTO countries(countryId,countryname)VALUES('+ cid +','+ name +')');
}

});
}
(function () { //alert('Im Ready'); var db = openDatabase('Test', '1.0', 'Test', 2 * 1024 * 1024); db.transaction(function (tx) { tx.executeSql('drop table countries '); tx.executeSql('CREATE TABLE IF NOT EXISTS countries (id INTEGER PRIMARY KEY AUTOINCREMENT,countryId,countryname)'); //alert('table created'); }); main(); }); function main() { if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } var url = "http://ws.geonames.org/searchJSON"; xmlhttp.open("GET", url, true); xmlhttp.send(); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { jsondata = xmlhttp.responseText; var json = eval('(' + jsondata + ')'); pushtoDB(json); } } } function pushtoDB(jsondata) { var db = openDatabase('Test', '1.0', 'Test', 2 * 1024 * 1024); var i = 0; db.transaction(function (tx) { for ( i = 0; i <= jsondata.geonames.length; i++) { var cid = jsondata.geonames[i].countryId; // Error:Cannot read property 'countryId'of undefined) var name = jsondata.geonames[i].lng; tx.executeSql('INSERT INTO countries (countryId,countryname) VALUES ("' + cid + '","' + name + '")'); } }); }


尝试:

Try:
 for ( i = 0; i < jsondata.geonames.length; i++) {
 
var cid = jsondata.geonames[i].countryId; // Error:Cannot read property 'countryId'of undefined)
            var name = jsondata.geonames[i].lng;
 
            tx.executeSql('INSERT  INTO countries (countryId,countryname) VALUES ("' + cid + '","' + name + '")');
 

        }



< = 更改为<



希望这会有所帮助。


Change the <= into <

Hope this helps.


这篇关于for循环中没有识别'i'值(错误行由注释行后跟错误表示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆