我正在尝试使用来自 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 正在正确创建,但是在列表项元素中,我得到了字符串“未定义”而不是表昵称列中的值。感谢您的帮助!