0

我为产品详细信息(productId、name、quantityonhand)创建了一个 Web 存储,并从服务器将记录填充到其中。我必须验证所需数量是否可用于接受订单。

产品表单为网络存储中可用的每个产品提供了复选框(名称=“产品”),并有一个相应的文本输入字段来接受所需的数量。

验证方法定义如下。

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 的异步特性,外部循环将被执行,并且仅对选择的最后一个产品进行验证。

帮我解决这个问题。我希望执行顺序发生。

尤维

4

1 回答 1

0

尝试在 db.transaction 的回调函数中将调用链接在一起:

function productValidation(){

    checkProductsSequential(document.Products.product, 0);
    return false;
}

function checkProductsSequential(products, i)
{
    if (i < products.length)
    {
        // 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;
                            }
                        }
                        checkProductsSequential(products, i + 1)
                    });
                }
            );  
        }
        else
        {
            checkProductsSequential(products, i + 1)
        }
    }
}
于 2010-08-18T01:34:34.997 回答