1

我是 HTML5 / JqueryMobile 的新手。我有这个函数,但由于某种奇怪的原因,它给了我第二次调用 Onclick() 的结果,这是我的代码:

function ListDBValues() {
  if (!window.openDatabase) { 
    alert('Databases are not supported in this browser.'); 
    return; 
 }
$('#lbUsers').html(''); 
db.transaction(function(transaction) { 
   transaction.executeSql('SELECT * FROM productos order by titulo;', [], 
   function(transaction, result) { 
      if (result != null && result.rows != null) { 
        for (var i = 0; i < result.rows.length; i++) { 
          var row = result.rows.item(i); 
          $('#lbUsers').append('<div id="producto"><img id="imgprod" width="100" src="images/' + row.foto +'">' + row.recid + '.' + row.titulo + '<br>$' + row.precio + ' MXP<br><input class="cuadro" type="button" id="cb.row" name="item"  value="ORDENAR" onclick=AddValueToOrders(' + row.recid + ');></div>');

        } 
      } 
     },errorHandler); 
 },errorHandler,nullHandler);
 return; 
}

这是第二个功能:

 function AddValueToOrders(item) {  
    if (!window.openDatabase) { 
      alert('Databases are not supported in this browser.'); 
      return; 
    } 
    msga = "El item es: "+ item ;
    alert(msga);
    db.transaction(function(tx) { 
        /// veo si ya existe /////

        tx.executeSql('SELECT count(*) AS c FROM orders where prodid = '+ item +'  ', [], 
        function (tx, result) { 
               totprod = result.rows.item(0).c;
               //totprod = results.rows.item(0)['c'];

        });
    });

    var themessage = "Total producto: ";
    themessage = themessage + totprod ;
    alert(themessage);                 

} 

问题是我想知道该产品是否已经存在于 Orders 表中,所以我可以更新它,而不是插入另一个具有相同代码的产品。

4

1 回答 1

0

不是真正的答案,但我重构了您的代码并添加了一些评论,您还应该查看我对您问题的原始评论,

function ListDBValues() {
    if (!window.openDatabase) { 
        alert('Databases are not supported in this browser.'); 
        return; 
    }
    $('#lbUsers').html(''); 
    db.transaction(function(transaction) { 
        transaction.executeSql('SELECT * FROM productos order by titulo;', [], 
        function(transaction, result) { 
            if (result != null && result.rows != null) { 
                for (var i = 0; i < result.rows.length; i++) { 
                    var row = result.rows.item(i);
                    // changed the append logic
                    // created new dom object w/jQuery
                    // bind tap event
                    // execute function on tap
                    // append to dom
                    $('<div id="producto"><img id="imgprod" width="100" src="images/' + row.foto +'">' + row.recid + '.' + row.titulo + '<br>$' + row.precio + ' MXP<br><input class="cuadro" type="button" id="cb.row" name="item"  value="ORDENAR"></div>').data( 'recid', row.recid ).bind('tap', function(){
                        AddValueToOrders( $(this).data('recid') );
                    }).appendTo('#lbUsers');
                } 
            } 
        }, errorHandler); 
    }, errorHandler, nullHandler);
    return; 
}
function AddValueToOrders(item) {  
    if (!window.openDatabase) { 
        alert('Databases are not supported in this browser.'); 
        return; 
    } 
    msga = "El item es: "+ item ;
    alert(msga);
    db.transaction(function(tx) { 
        /// veo si ya existe /////
        tx.executeSql('SELECT count(*) AS c FROM orders where prodid = '+ item +'  ', [], 
        function (tx, result) { 
            totprod = result.rows.item(0).c;
            //totprod = results.rows.item(0)['c'];
            alert("Total producto: " + totprod);
            // you can do your insert here depending on 
            // the value you got back from this checkpoint
        });
    });
}

请让我知道这是否更好。

更新: 您也在 for 循环中创建的元素上使用 ID,您应该知道这会产生冲突,因为 ID 是唯一的,您可能不想将它们更改为类

于 2012-05-29T20:05:42.337 回答