2012-12-04 34 views
1

我想加載一個外部頁面(所以沒有使用Ajax從查詢手機),問題是,當我使用一個功能激活通過點擊一個鏈接標籤,和在這個功能我有一個插入到數據庫,鏈接跟隨前腳本寫入DB ..phonegap jquerymobile - 加載一個頁面,當異步的東西完成

這裏的部分代碼:

$('#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的,但插入到asyncronous調用數據庫的記錄不被執行,../client/consultClients.html頁面加載

如何我可以讓它跟着鏈接後工作完成?

回答

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; 

} 

泰添爲您支持...

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

+0

好了,鏈接被關注,但數據庫沒有更新,我正在嘗試一些類似於其他類似主題中的建議,但沒有得到它的工作...... – Gianluca

+0

看看這個問題:http:// stackoverflow。 com/questions/3903155 /同步查詢到Web-SQL數據庫 –

+0

哇我剛剛發現了一個全新的世界,關閉..我需要研究它導致1小時前我甚至不知道它存在..順便說一句,如果任何人都願意拋出一些代碼作爲封閉的例子,這將不勝感激! – Gianluca