2012-05-29 32 views
1

我是新的HTML5/JqueryMobile。 我有这个功能,但对于一些奇怪的原因,它给我的结果我第二次打电话,用的onClick(),这里是我的代码:Jquery/Html5函数问题双击问题

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);     

} 

事情是我想知道如果该产品已经存在于订单表,所以我可以更新它,而不是插入相同的代码的另一个产品。

+0

不知道为什么你参考单击,双击,如果你是移动你不会使用自来水事件?我可以从你的代码读取你正在做一些交易,你应该做的回调里面快讯函数,它们是异步的,不会遵循其他函数逻辑的执行顺序。 请更新你的答案,更好地描述你正在做什么,你期待。 – rroche

回答

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 
     }); 
    }); 
} 

请让我知道这工作得更好。

UPDATE: 你也正在使用的ID在您创建的for循环的元素,你应该知道这会造成冲突,ID的意图是独一无二的,你可能会wan't将其更改为类

+0

嗨,非常感谢,它像我期待的那样工作。你说得对,也许我没有像移动应用程序那样思考,我想像一个网络应用程序,所以我需要学习更多...,再次感谢 – CaribeSoft

+0

很高兴我能帮助,祝你好运 – rroche