Jquery/Html5函数问题双击问题
我是HTML5/JqueryMobile的新手。 我有这个函数,但出于某种奇怪的原因,它在我第二次调用时给出了结果,通过Onclick,下面是我的代码: 函数ListDBValues{ if!window.openDatabase{ 警报“此浏览器不支持数据库。”; 回来 } $'lbUsers'.html; db.transactionfunctiontransaction{ transaction.executeSql'SELECT*FROM productos order by titulo;',[], functiontransaction,结果{ 如果result!=null&&result.rows!=null{ 对于变量i=0;iJquery/Html5函数问题双击问题,jquery,html,jquery-mobile,Jquery,Html,Jquery Mobile,我是HTML5/JqueryMobile的新手。 我有这个函数,但出于某种奇怪的原因,它在我第二次调用时给出了结果,通过Onclick,下面是我的代码: 函数ListDBValues{ if!window.openDatabase{ 警报“此浏览器不支持数据库。”; 回来 } $'lbUsers'.html; db.transactionfunctiontransaction{ transaction.executeSql'SELECT*FROM productos order by titul
问题是我想知道Orders表中是否已经存在该产品,这样我就可以更新它,而不是插入另一个相同代码的产品。这不是一个真正的答案,但我重构了您的代码并添加了一些注释,您还应该检查我对您的问题的原始注释
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是唯一的,您可能不想将它们更改为类不确定为什么要引用click和双击,如果您在移动设备上,您不会使用tap事件吗?,我可以从您的代码中看出,您正在执行一些事务,您应该在回调函数中执行警报,这些警报是异步的,不会遵循函数逻辑其余部分的执行顺序。请更新您的答案,更好地描述您正在做的事情以及您的期望。您好,非常感谢,它就像我期望的那样工作。你是对的,也许我的思维方式不像移动应用,我的思维方式像网络应用,所以我需要学习更多…,再次感谢