2012-02-27 41 views
0

先通過代碼:「$找不到變量」: -收到錯誤:在jQuery Mobile的

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
    <meta name="viewport" content="width=default-width; user-scalable=no" /> 
    <meta http-equiv="Content-type" content="text/html;charset=utf-8"> 
    <title>Embedded Sql Example</title> 

    <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></ script> 


    <script type="text/javascript" charset="utf-8" src="jquery.min.js"></script> 

<!-- main scripts used in this example --> 
<script type="text/javascript" charset="utf-8"> 
// global variables 
var db; 
var shortName = 'WebSqlDB'; 
var version = '1.0'; 
var displayName = 'WebSqlDB'; 
var maxSize = 65535; 
// this is called when an error happens in a transaction 
function errorHandler(transaction, error) { 
    alert("Error: "+ error.message + "code: " + error.code); 
} 
// this is called when a successful transaction happens 
function successCallBack() { 
    alert("DEBUGGING: success"); 
} 
function nullHandler(){} 
// called when the application loads 
function onBodyLoad(){ 
// This alert is used to make sure the application is loaded correctly 
alert("DEBUGGING: we are in the onBodyLoad() function"); 
if (!window.openDatabase) { 

    alert("Databases are not supported in this browser."); 
    return; 
} 

db = window.openDatabase(shortName, version, displayName,maxSize); 

db.transaction(function(tx){ 


    tx.executeSql('CREATE TABLE IF NOT EXISTS User(UserId INTEGER NOT NULL PRIMARY KEY, FirstName TEXT NOT NULL, LastName TEXT NOT NULL)', [],nullHandler,errorHandler); 
},errorHandler,successCallBack); 
} 

function ListDBValues() { 
if (!window.openDatabase) { 
    alert("Databases are not supported in this browser."); 
    return; 
} 
    $('#lbUsers').html(''); 

db.transaction(function(transaction) { 
    transaction.executeSql('SELECT * FROM User;', [], 
    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("<br>" + row.UserId + ". " + row.FirstName+ " " + row.LastName); 
     } 
     } 
    },errorHandler); 
},errorHandler,nullHandler); 
return; 
} 

function AddValueToDB() { 
if (!window.openDatabase) { 
    alert("Databases are not supported in this browser."); 
    return; 
} 

    db.transaction(function(transaction) { 
    transaction.executeSql('INSERT INTO User(FirstName, LastName) VALUES (?,?)',[$('#txFirstName').val(), $('#txLastName').val()],nullHandler,errorHandler); 
    }); 

ListDBValues(); 
return false; 
} 
</script> 
</head> 
<body onload="onBodyLoad()"> 
<h1>WebSQL</h1> 
<input id="txFirstName" type="text" placeholder="FirstName"> 
<input id="txLastName" type="text" placeholder="Last Name"><br> 
<input type="button" value="Add record" onClick="AddValueToDB()"> <br> 
<input type="button" value="Refresh" onClick="ListDBValues()"> <br> 
<br> 
<span style="font-weight:bold;">Currently stored values:</span> 
<span id="lbUsers"></span> 
</body> 
</html> 

我在PhoneGap的應用程序的工作,我在Android os.I正在創建它越來越

"Can't find variable: $ at file:///android_asset/www/dbDemo.html:103" 

線,由於錯誤發生在103

$('#lbUsers').html(''); 

在運行上述code.Help我擺脫problem.Any幫助將是非常appreciated.Thanx提前
"Can't find variable: $ at file:///android_asset/www/dbDemo.html:131" 

線,由於錯誤發生在131

transaction.executeSql('INSERT INTO User(FirstName, LastName) VALUES (?,?)',[$('#txFirstName').val(), $('#txLastName').val()],nullHandler,errorHandler); 

+0

你確定你的jQuery加載正確嗎?如果是的話,哪兩條線(103和131)會拋出錯誤? – m90 2012-02-27 07:35:00

+0

103: - $('#lbUsers')。html(''); (','),[$('#txFirstName')。val(),$('#txLastName')。val() ],nullHandler,的ErrorHandler); – himanshu 2012-02-27 07:36:33

+1

我認爲這可能有助於http://stackoverflow.com/questions/4888725/jquery-referenceerror-cant-find-variable – 2012-02-27 07:39:06

回答

3

它看起來像jQuery沒有加載。我看你的代碼,我認爲這個問題可能是作爲一個額外的空間,簡單...

<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></ script> 
<script type="text/javascript" charset="utf-8" src="jquery.min.js"></script> 

有在關閉腳本標記是一個空間在那裏 - 嘗試從</ script>改變它</script>

+0

Thanx哥們你抓到我愚蠢的錯誤.... :) :) – himanshu 2012-02-28 04:57:53

0

嘗試使用jQuery('blabla')代替$('blabla')

+0

仍然得到相同的錯誤.... – himanshu 2012-02-27 08:15:28

+0

你能檢查jquery是否被加載?像'console.log(jQuery)' – silly 2012-02-27 08:30:36

0

要調用ListDBValues()head部分(第二線收盤script標記之前)。這會導致問題,因爲jQuery庫尚未加載,這就是爲什麼你會收到錯誤。相反,嘗試在該函數調用移動到你body - 收盤body標記之前插入以下內容:

<script type="text/javascript"> 
$(document).ready(function() { 
    ListDBValues(); 
}); 
</script> 
+0

仍然得到相同的錯誤.... – himanshu 2012-02-27 08:15:10

0

嘗試在如下所示的phonegap-1.4.1.js腳本標記之前放置jquery.min.js腳本標記。

好像在使用前的jQuery未加載。

+0

anil 2014-04-17 09:10:20