在javascript中动态检索值

时间:2013-05-16 06:03:45

标签: javascript sqlite cordova

我正在处理手机间隙应用程序,我想检索元素的数据,说元素id 1有四个值。我想从sqllite数据库中检索它并在幻灯片视图上设置它。

我暂时使用了以下代码,我得到了值,但对于每个元素,我分别设置了值,

    var instance= results.rows.length;
    for (var i=0; i<instance; i++)
    {
        cidinstance=results.rows.item(i).cid;
        var key=results.rows.item(i).key;
        var valueinstance=results.rows.item(i).value;
        console.log("cid= "+cidinstance +   "key= "+key);   
        console.log("cid= "+cidinstance +   "valueinstance= "+valueinstance);
    }
   if(val[1]==0) 
   {
        document.getElementById("s1cause").innerHTML=results.rows.item(0).value;
        document.getElementById("s2cause").innerHTML=results.rows.item(1).value;
        document.getElementById("s3cause").innerHTML=results.rows.item(2).value;
        document.getElementById("s4cause").innerHTML=results.rows.item(3).value;
        document.getElementById("s5cause").innerHTML=results.rows.item(4).value;
    }
    else if(val[1]==1)
    {
        document.getElementById("s1cause").innerHTML=results.rows.item(4).value;
        document.getElementById("s2cause").innerHTML=results.rows.item(5).value;
    }
    else if(val[1]==2)
    {
        document.getElementById("s1cause").innerHTML=results.rows.item(6).value;
        document.getElementById("s2cause").innerHTML=results.rows.item(7).value;
        document.getElementById("s3cause").innerHTML=results.rows.item(8).value;
    }

通过这种方式,我以静态方式设置值。我想动态获取这些值。

我的数据库将以此格式显示。

id  cid values

1   1   value1
2   1   value2
3   1   value3

请指导我。我该如何实施?

同时在等待您的建议时,我对代码进行了一些更改并尝试执行它。这是我尝试过的代码

value=val[1];
var causedetails = results.rows.length;
for (var i=value; i<causedetails; i++)
{
    cidinstance=results.rows.item(i).cid;
    console.log("cid= "+cidinstance );
    var valueinstance=results.rows.item(i).value;
    //document.getElementById("s1cause").innerHTML=valueinstance;
    console.log("cid= "+cidinstance  + "" + "valueinstance= "+valueinstance);
}

当val [1} = 0时,它完美地运行了所有值。

问题从以下情况开始,当val [1] = 1或2时,....当val [1] = 1时,它实际上只有2个值。但是当我打印它时在控制台日志中,在看到错误之后看到了1个值

JSCallback: Message from Server:  SQLitePluginTransaction.queryCompleteCallback(value1,value2)--->(the 2 values that should have come in the console log).

错误在哪里,请指导我。

console.log打印出来

当值= 0时

05-16 13:42:59.062: I/Web Console(10351): cid= 1 valueinstance= VALUE 1 of cid 1 at file:///android_asset/www/abc.html?var%20id=0:181

05-16 13:42:59.062: I/Web Console(10351): cid= 1 valueinstance= VALUE 2 of cid 1 at file:///android_asset/www/abc.html?var%20id=0:181

05-16 13:42:59.062: I/Web Console(10351): cid= 1 valueinstance= VALUE 3 of cid 1 at file:///android_asset/www/abc.html?var%20id=0:181

05-16 13:42:59.062: I/Web Console(10351): cid= 1 valueinstance= VALUE 4 of cid 1 at file:///android_asset/www/abc.html?var%20id=0:181

当值= 1

05-16 13:49:36.765: I/Web Console(11184): cid= 2 valueinstance= VALUE 2 of cid 2 at file:///android_asset/www/abc.html?var%20id=0:181

05-16 13:49:36.769: I/Web Console(11184): JSCallback: Message from Server:  SQLitePluginTransaction.queryCompleteCallback('1368692376635000','1368692376639000', [{"value":"VALUE 1","id":"5","cid":"2"},{"value":"VALUE 2","id":"6","cid":"2"}]); at file:///android_asset/www/cordova-2.1.0.js:3726

cordova2.1.js中的SCRIPT是

function startXhr() {
    // cordova/exec depends on this module, so we can't require cordova/exec on the module level.
    var exec = require('cordova/exec'),
    xmlhttp = new XMLHttpRequest();

    // Callback function when XMLHttpRequest is ready
    xmlhttp.onreadystatechange=function(){
        if (!xmlhttp) {
            return;
        }        
if (xmlhttp.readyState === 4){
                    // If callback has JavaScript statement to execute
                    if (xmlhttp.status === 200) {

                        // Need to url decode the response
                        var msg = decodeURIComponent(xmlhttp.responseText);
                        setTimeout(function() {
                            try {
                                var t = eval(msg);
                            }
                            catch (e) {
                                // If we're getting an error here, seeing the message will help in debugging
    LINE 3726==>                console.log("JSCallback: Message from Server: " + msg);
                                console.log("JSCallback Error: "+e);
                            }
                        }, 1);
                        setTimeout(startXhr, 1);
                    }

有人可以帮助我吗???,请

1 个答案:

答案 0 :(得分:1)

您可以使用一些执行可重复部分代码的函数:

function setValues(count, offset) {
    var i;
    offset = offset || 0;
    for(i=0; i<count; i++) {
        document.getElementById("s"+(i+1)+"cause").innerHTML=results.rows.item(i+offset).value;
    }
}

该函数的第一个参数是id匹配模式s<positive number>cause的元素数,第二个参数是results.rows中应从中检索数据的起始索引。< / p>

比你的代码看起来像这样:

if(val[1]===0) {
    setValues(5);
} else if(val[1]===1) {
    setValues(2,4);
} else if(val[1]===2) {
    setValues(3,6);
}