如何在phoneGap API中使JavaScript变量全局 [英] How to make javascript variable global in phoneGap api

查看:74
本文介绍了如何在phoneGap API中使JavaScript变量全局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习如何使用phonegap,并且学习进展顺利,但是我在phoneGap Api中使用了全局变量.实际上,首先我要从xml文件中获取结果,然后将数据插入数据库中.我已经完成了这项工作,但是现在我想获取插入行的最后一个id,然后插入到另一个表中,因为在xml中有子节点.

I am learning how to use phonegap now and learning is going great but I'm stuck with global variable in phoneGap Api. Actually First I'm getting the result from xml file and then insert the data into Database. I have done this work but now I want to get the last id of inserted row and then insert into another table because in xml there are sub nodes.

function populateDB(tx) {
        tx.executeSql('DROP TABLE IF EXISTS fruit');
        tx.executeSql('DROP TABLE IF EXISTS fruit_benefit');
        tx.executeSql('CREATE TABLE IF NOT EXISTS fruit (id INTEGER NOT NULL PRIMARY KEY, fname,fsname,fruit_icon,fruit_image)');
        tx.executeSql('CREATE TABLE IF NOT EXISTS fruit_benefit (id INTEGER NOT NULL PRIMARY KEY, benefit, fruit_id)');
        $.ajax({
        type: "GET",
        url: "data.xml",
        dataType: "xml",
        success: function(xml) {
            $(xml).find('fruit').each(function(){
                var fname = $(this).find('fname').text();
                var fsname = $(this).find('fsname').text();
                var fruit_icon = $(this).find('fruit_icon').text();
                var fruit_image = $(this).find('fruit_image').text();
                //$('<div class="items" id="link_'+fname+'"></div>').html('<a href="'+fsname+'">'+fname+'</a>').appendTo('#lbUsers');
                db.transaction(function(transaction) { 
                    transaction.executeSql('INSERT INTO fruit (fname,fsname,fruit_icon,fruit_image) VALUES (?,?,?,?)',[fname, fsname, fruit_icon, fruit_image],function(transaction, results){
               var lastId = results.insertId; 
            },nullHandler,errorHandler);
              });

              alert(lastId);

            });
        }
    });

}

相关行是:

var lastId = results.insertId; //The last inserted id is shown here.

alert(lastId); //I want to show last inserted id here

我已经尝试了JavaScript全局变量和本地存储,但是都无法正常工作.

I have tried both a JavaScript global variable and local storage but neither are working.

推荐答案

为此,您甚至不需要它是全局的.在函数中但在foreach之前定义lastId.如果那不起作用,则通过移动var lastId使其真正成为全局属性.函数populateDB()之前的行

You don't even need it to be global for this. Define the lastId in the function but before the foreach. If that doesn't work, then making it global should really work, by moving the var lastId; line before function populateDB()

function populateDB(tx) {
        tx.executeSql('DROP TABLE IF EXISTS fruit');
        tx.executeSql('DROP TABLE IF EXISTS fruit_benefit');
        tx.executeSql('CREATE TABLE IF NOT EXISTS fruit (id INTEGER NOT NULL PRIMARY KEY, fname,fsname,fruit_icon,fruit_image)');
        tx.executeSql('CREATE TABLE IF NOT EXISTS fruit_benefit (id INTEGER NOT NULL PRIMARY KEY, benefit, fruit_id)');
        $.ajax({
        type: "GET",
        url: "data.xml",
        dataType: "xml",
        success: function(xml) {

            var lastId;

            $(xml).find('fruit').each(function(){
                var fname = $(this).find('fname').text();
                var fsname = $(this).find('fsname').text();
                var fruit_icon = $(this).find('fruit_icon').text();
                var fruit_image = $(this).find('fruit_image').text();
                //$('<div class="items" id="link_'+fname+'"></div>').html('<a href="'+fsname+'">'+fname+'</a>').appendTo('#lbUsers');
                db.transaction(function(transaction) { 
                    transaction.executeSql('INSERT INTO fruit (fname,fsname,fruit_icon,fruit_image) VALUES (?,?,?,?)',[fname, fsname, fruit_icon, fruit_image],function(transaction, results){
               lastId = results.insertId; 
            },nullHandler,errorHandler);
              });

              alert(lastId);

            });
        }
    });

}

这篇关于如何在phoneGap API中使JavaScript变量全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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