我是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表中是否已存在该产品,因此我可以更新它,而不是插入相同代码的其他产品。
答案 0 :(得分: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是唯一的,你可能不会把它们改成类