2

我正在尝试使用来自 web sql 数据库的数据来填充 jqm 应用程序的 html 标记。这是我的代码:

 $("#profile").on("pageinit", function () {
    db.transaction(function (transaction) {
        var sql = "CREATE TABLE IF NOT EXISTS profile " +
            " (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
            "nickname VARCHAR(100) NOT NULL," +
            "tankVol VARCHAR(100) NOT NULL," +
            "waterType VARCHAR(100) NOT NULL," +
            "category VARCHAR(100) NOT NULL)"
        transaction.executeSql(sql, undefined);

        var sqlNickname = "SELECT nickname FROM profile";
        transaction.executeSql(sqlNickname, undefined, function (transaction, result) {
            var ul = "<ul>";
            if (result.rows.length) {
                for (var i = 0; i < result.rows.length; i++) {
                    var row = result.rows.item;
                    var nickname = row.nickname;
                    ul += "<li>" + nickname + "</li>";
                    ul += "</ul>";

                }
            }
            var $profileNickname = $("#profile div:jqmData(role=content)");
            $profileNickname.html(ul);
            var $nicknameContent = $profileNickname.find("ul");
            $nicknameContent.listview();
        }, error);
    });
});

我正在使用的表将始终只有一条记录,因此我不确定是否需要使用 for 循环遍历表。我相信问题出在这条线上:

transaction.executeSql (sqlNickname, undefined,

因为表工作正常并且 html 正在正确创建,但是在列表项元素中,我得到了字符串“未定义”而不是表昵称列中的值。感谢您的帮助!

4

1 回答 1

0

First of all, you are closing ur ul tag inside the loop. If there are more then 1 entry in the resultset, you'll get several closing tags for one opening tag. Place that closing tag outside the loop.

But the error you are getting is this:

var row = result.rows.item;

Change this to:

var row = result.rows.item(i);

So the program knows which row to take from your resultset. Even for a single result this is necessary.

于 2013-05-06T12:18:52.867 回答