製品の詳細 (productId、name、quantityonhand) の Web ストレージを作成し、サーバーからレコードを入力しました。注文を受け入れるために必要な数量が利用可能かどうかを検証する必要があります。
製品フォームには、Web ストレージで利用可能なすべての製品のチェックボックス (name="product" を含む) と、必要な数量を受け入れるための対応するテキスト入力フィールドがあります。
検証方法は次のように定義されています。
function productValidation(){
for(i=0;i<document.Products.product.length;i++){
// checking whether the product has been checked / choosen
if (document.Products.product[i].checked==true){
var productId = document.Products.product[i].value;
var qty = document.Products.p[i].value;
var db = systemDB;
// validating the available quantity
db.transaction(
function(transaction){
transaction.executeSql('select * from product where productId=?;',
[productId],
function(transaction, results){
for (var j=0; j<results.rows.length; j++) {
var row = results.rows.item(j);
if (qty>row['qoh']){
alert(
row['productname']
+ ' is out of stock. We can serve you only '
+ row['qoh'] + ' quantities currently!');
document.Products.product[i].checked = false;
document.Products.p[i].value = 0;
}
}
});
}
);
}
}
return false;
}
このコードが実行されると、db.transaction の非同期性により、外側のループが実行され、最後に選択された製品に対してのみ検証が行われます。
これを修正するのを手伝ってください。実行を順番に行いたい。
ユヴィ