简体   繁体   English

jQuery Mobile数据库成功事件未触发功能

[英]JQuery Mobile db success event not triggering function

I am having a problem executing a success function within multiple success events, let me show you the code: 我在多个成功事件中执行成功函数时遇到问题,让我向您展示代码:

$("#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');
        }
        } );
});

As you can imagine I have an UL with LI that gets added when the ADD button is clicked, everything works fine, including the DB executions. 可以想象,我有一个带有LI的UL,当单击ADD按钮时会添加它,一切正常,包括DB执行。

The problem is when I click the $("#view_ben_"+benid) it does run properly, but it doesn't execute the SELECT success function properly although it does show a the console.log set outside the success function. 问题是,当我单击$(“#view_ben _” + benid)时,它可以正常运行,但是尽管它确实显示了成功函数之外的console.log集,但它不能正确执行SELECT成功函数。 Anyone has any ideas? 有人有什么想法吗?

Ok, I figured it out: On the following line you are closing the executeSql function directly, leaving out the rest of the arguments unused in the function body. 好的,我知道了:在下面的行中,您直接关闭了executeSql函数,而executeSql了函数主体中未使用的其余参数。 In other words: There's a bracket missing ;). 换句话说:没有一个括号;)。

tx.executeSql('SELECT * FROM Beneficiarios WHERE ID = '+benid),

Change your syntax to the following example: 将语法更改为以下示例:

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

I'd like to point out that you are cramming too many functions into each other. 我想指出的是,您彼此之间的功能过多。 Consider outsourcing binding, success and error functions to the top level to prevent Inception like Matryoshka code. 考虑将绑定,成功和错误功能外包到顶层,以防止像Matryoshka代码这样的Inception。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM