我在多个成功事件中执行成功功能时遇到问题,让我向您展示代码:
$("#beneficiarios").one("pagebeforeshow", function(){
db.transaction( function(tx){ queryDBAll(tx, "Beneficiarios", 0, 10) }, onError ); //LOAD BENEFICIARIOS IN DB
$("#btn-add-ben").click(function(){
var nombres = $('#b_nombres').val();
var apellidos = $('#b_apellidos').val();
var dia = $('#b_dia').val();
var mes = $('#b_mes').val();
var año = $('#b_año').val();
var fecha_nac = año+"-"+mes+"-"+dia;
//ADD BENEFICIARIO TO DB
db.transaction( function(tx){
tx.executeSql("INSERT INTO Beneficiarios (nombres, apellidos, fecha_nac) VALUES ('"+nombres+"','"+apellidos+"','"+fecha_nac+"')",
[],
function(tx, results){
var benid = results.insertId;
$('#emptyBeneficiarios').remove();
$('#ul-beneficiarios').append(
'<li><a href="#" id="view_ben_'+benid+'">'+nombres+' '+apellidos+'</a><a href="#" id="del_ben_'+benid+'" data-position-to="window" data-icon="delete" class="itemDelete">Eliminar</a></li>').listview('refresh');
$("#del_ben_"+benid).click(function(){
//PENDING: CONFIRM DIALOG
db.transaction( function(tx){
tx.executeSql("DELETE FROM Beneficiarios WHERE ID = "+benid),
[],
console.log(benid+" borrado con éxito!"),
onError
});
});
$("#view_ben_"+benid).click(function(){
db.transaction( function(tx){
tx.executeSql('SELECT * FROM Beneficiarios WHERE ID = '+benid),
[],
//SUCCESS ACTION! -- NOT WORKING
function(tx, results) {
$('#b_nombres').val(results.rows.item.nombres);
$('#b_apellidos').val();
$('#b_dia').val();
$('#b_mes').val();
$('#b_año').val();
console.log(benid+" shown successfully!") //IS NOT SHOWN AT ALL
}
console.log(benid+" mostrado shown successfully without function!") //SHOWN AS DESIRED
,
onError
});
});
},
onError );
},
onError );
});
$( "#ul-beneficiarios" ).delegate( "li a", "click", function() {
$(this).parent("li").remove();
var li_num = $('#ul-beneficiarios li').length;
if (li_num == 1) {
addEmptyBeneficiarios('#ul-beneficiarios');
}
} );
});
你可以想象我有一个带有LI的UL,当点击ADD按钮时它会被添加,一切正常,包括数据库执行。
问题是当我单击$(“#view_ben _”+ benid)它确实正常运行时,但它没有正确执行SELECT success函数,尽管它确实显示了在success函数之外设置的console.log。有人有什么想法吗?
答案 0 :(得分:0)
好的,我弄清楚了:
在下一行中,您将直接关闭executeSql
函数,而忽略函数体中未使用的其余参数。换句话说:缺少一个括号;)。
tx.executeSql('SELECT * FROM Beneficiarios WHERE ID = '+benid),
将语法更改为以下示例:
db.transaction( function(tx){
tx.executeSql(
("DELETE FROM Beneficiarios WHERE ID = "+benid), // SQL COMMAND
[], // NO IDEA WHAT THAT IS FOR
console.log("success"), // SUCCESS CALLBACK
onError // ERROR CALLBACK
);
});
我想指出,你们正在把太多的功能塞进彼此。考虑将绑定,成功和错误函数外包到顶层以防止像Matryoshka代码一样的Inception。