我正在写一个phonegap应用程序。 我想要做的是同时更新多个记录。我在下面发布的代码会在订单审核屏幕上显示。如果用户更新了qty,则表行将添加更改的类。然后,当用户点击更新按钮时,下面的代码将触发。
对于我们的示例,我们假设用户更改了3个订单项的数量。所有3个日志正确,并且所有3个日志成功的事务。但是,只有3个中的最后一个实际保存到数据库中。我在这做错了什么。
$('#update').click(function(){
//update records where the tr has the changed class
$('#log').append("<p>Update Clicked</p>");
$('.changed').each(function(){
order.Id=$(this).attr('id');
item.qty=$(this).children().children().filter('#itemQTY').val();
item.bercor=$(this).children().filter('#bercor').text();
$('#log').append("<p>item.qty= " + item.qty + " and order.Id = " + order.Id + "item.bercor = " + item.bercor + " </p>" );
//All three log correctly
db.transaction(function(tx){
tx.executeSql('update orderItems set qty=? where bercor=?',[item.qty,item.bercor],null,errorCB);
},errorCB,successCB);
//All three log success
});
});
如果需要,我可以发布更多代码。在此先感谢您的帮助。
答案 0 :(得分:2)
尝试将交易移到each
之外,如下所示:
$('#update').click(function(){
//update records where the tr has the changed class
$('#log').append("<p>Update Clicked</p>");
db.transaction(function(tx){
$('.changed').each(function(){
order.Id=$(this).attr('id');
item.qty=$(this).children().children().filter('#itemQTY').val();
item.bercor=$(this).children().filter('#bercor').text();
$('#log').append("<p>item.qty= " + item.qty + " and order.Id = " + order.Id + "item.bercor = " + item.bercor + " </p>" );
//All three log correctly
tx.executeSql('update orderItems set qty=? where bercor=?',[item.qty,item.bercor],null,errorCB);
//All three log success
});
},errorCB,successCB);
});