[英]How to use jquery IF statement instead of FOR
我收到了對 sqlite 的請求並使用以下代碼接收數據:
var rows = results.rows;
alert(rows.length);
for (var index = 0; index < rows.length; index++) {
var x = rows.item(index);
$('body').append(x.id + ',' + x.username + ',' + x.time + ',' + x.distance);
}
但是如何檢查和提醒是否有 0 行,我的意思是表中沒有數據?
應該是???:
var rows = results.rows;
if(rows.length===0) { alert('nothing in DB');
} else {
var x = rows.item(index);
$('body').append(x.id + ',' + x.username + ',' + x.time + ',' + x.distance);
}
非常感謝。
為了檢查結果集中是否缺少行,使用rows.length
就足夠了,如下所示:
if (rows.length === 0) {
alert("There are no rows.");
}
FOR 循環的一個很酷的事情是:如果 rows.length = 0(你沒有從數據庫中返回任何行)FOR 循環甚至不會迭代 1 次 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.