1

我想加载一个外部页面(因此不使用查询移动设备中的 ajax),问题是当我使用通过单击链接标签激活的功能时,并且在此功能中我有一个 INSERT 到数据库中,链接是在脚本写入数据库之前跟随..

这是代码的一部分:

$('#aggiungiClienteRubrica').click(function() {

                               db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
                               db.transaction(function(tx){
                                              var sql = 'INSERT INTO CLIENTI (nome, cognome) VALUES ("'+$('#nome').val()+'", "'+$('#cognome').val()+'")';
                                              tx.executeSql(sql)}, errorCB);
                               });

在 html 文件中,我只有一个这样的标签:

<a id="aggiungiClienteRubrica" href="../client/consultClients.html" data-role="button" data-theme="b" rel="external">Add client</a>

所以问题是,我可以执行javascript,但是异步调用将插入到db中的记录没有被执行并且../client/consultClients.html页面被加载

工作完成后如何让它跟随链接?

4

2 回答 2

0

试试这个:


$('#aggiungiClienteRubrica').click(function(e) {
    e.preventDefault(); // will stop default link action

    // capture the link url
    var url = $(this).attr('href');

    // your DB stuff here, wait for completion
    // (I'm not familiar with local DB so don't know that part)

    // use this if you managed to do the above synchronously
    // or in a 'success' callback
    window.location.href = url;
});

于 2012-12-04T09:15:16.017 回答
0

好的,成功了!!!

就是这样:

$('#aggiungiClienteRubrica').click(function(e) {
                               var myVar = click(e);
                               myVar;
});

然后点击函数定义如下:

function click(e){

db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
e.preventDefault();
db.transaction(function(tx){
               var sql = 'INSERT INTO CLIENTI (nome, cognome) VALUES ("'+$('#nome').val()+'", "'+$('#cognome').val()+'")';
               tx.executeSql(sql)}, errorCB, successCreation);
}

然后,定义回调函数successCreation:

function successCreation(){
var href = $('#aggiungiClienteRubrica').attr('href');
window.location.href = href;

}

蒂蒂姆的支持...

于 2012-12-04T16:59:50.283 回答