我正在使用带有phonegap的opendatabase,并且我的桌面上的Chrome浏览器中的一切正常但是当我在我的Android设备上运行它并点击调用insertRecord()的按钮时它说没有使用eclipse我得到错误
10-26 10:37:13.191: E/Web Console(12134): Uncaught ReferenceError: insertRecord is not defined at file:///android_asset/www/index.html:137
我的代码看起来像这样
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
var minChange;
var pumpBore;
var sInches;
var sMin;
var maxEfficiency;
var field;
var percent;
var calcSlide;
var results;
var id;
var listing;
var slider_value;
var getMin;
var dateDay = "Wed";
var dateNum = "22";
var date = new Date();
var dateDay = date.getDay();
var dateNum = date.getDate();
var hour = date.getHours();
var min = date.getMinutes();
var dateTime = hour + ":" + min;
pumpBore = $("#pumpBore").val();
sInches = $("#sInches").val();
sMin = $("#sMin").val();
calcSlide = $("#calcSlide").val();
field = $('#field').val();
results = $('#results');
id = $('#id');
getMin = $('#slider-1').val();
// FUNCTION TO CALCULATE AND DISPLAY PRODUCTION
function calculate(sMin) {
if (sMin == null)
sMin = sMin
pumpBore = $("#pumpBore").val();
sInches = $("#sInches").val();
calcSlide = $("#calcSlide").val();
field = $('#field').val();
console.log("Calc pump Bore: " + pumpBore);
console.log("Calc Inches: " + sInches);
console.log("Calc Min: " + sMin);
console.log("Field Amount: " + field);
console.log("Percentage: " + percent);
console.log('Time: ' + dateTime);
maxEfficiency = Math.round(parseFloat(pumpBore) * parseFloat(sInches)
* parseFloat(sMin));
console.log("Max Efficiency " + maxEfficiency);
// console.log(calcSlide);
$("#sMin").val(sMin);
$("#barrels").val(maxEfficiency);
percent = Math.round(parseFloat(field) / parseFloat(maxEfficiency) * 100);
$('#percent').val(percent + "%");
console.log("========================================");
sMin = $('#sMin').val();
} // eof calculate
function consoleList() {
console.log('============================');
console.log("pump Bore: " + pumpBore);
console.log("Stroke Inches: " + sInches);
console.log("Strokes Per Minute: " + $('#sMin').val());
console.log("Max Efficiancy: " + maxEfficiency);
console.log("Field: " + field);
console.log("Field %: " + percent);
console.log('Time: ' + dateTime);
console.log('Slider Value: ' + slider_value);
console.log('============================');
console.log(results);
}
var strokeMin = $('#sMin').val();
var createStatement = "CREATE TABLE IF NOT EXISTS Notes (id INTEGER PRIMARY KEY AUTOINCREMENT, dateDay TEXT, dateNum TEXT, dateTime TEXT, pumpBore TEXT, sInches TEXT, sMin TEXT, maxEfficiency TEXT, field TEXT, prodPercent TEXT)";
var selectAllStatement = "SELECT * FROM Notes ORDER BY id DESC";
var insertStatement = "INSERT INTO Notes (dateDay, dateNum, dateTime, pumpBore, sInches, sMin, maxEfficiency, field, prodPercent) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
var deleteStatement = "DELETE FROM Notes WHERE id=?";
var dropStatement = "DROP TABLE Notes";
var insertValues = "[pumpBore.value, sInches.value, sMin.value, maxEfficiency.value, field.value, percent.value]";
var testInsertValues = '["123", "234", "567", "657", "753", "2654"]';
var db = openDatabase("noteList", "1.0", "Note List", 500000);
var dataset;
createTable();
function onError(tx, error) {
alert(error.message);
}
function showRecords() {
results.html('');
db.transaction(function(tx) {
tx.executeSql(selectAllStatement, [], function(tx, result) {
dataset = result.rows;
for ( var i = 0, item = null; i < dataset.length; i++) {
item = dataset.item(i);
listing = '<div class="noteContainer">'
+ '<div class="dateContainer">'
+ '<span id="weekDay" class="">'
+ item['dateDay']
+ '</span>'
+ '<span id="date" class="">'
+ item['dateNum']
+ '</span>'
+ '<span id="time" class="">'
+ item['dateTime']
+ '</span>'
+ '</div><!-- eof dateContainer -->'
+ '<div class="specContainer">'
+ '<span class="">pump Bore: '
+ item['pumpBore']
+ '</span>'
+ '<span class="">Stroke Length: '
+ item['sInches']
+ '</span>'
+ '<span class="">Stroke/Min: '
+ item['sMin']
+ '</span>'
+ '<span class="">Barrels: '
+ item['maxEfficiency']
+ '</span>'
+ '<span class="">Field Prod: '
+ item['field']
+ '</span>'
+ '<span class="">Production %: '
+ item['prodPercent']
+ '</span>'
+ '</div><!-- eof specContainer -->'
+ '<div class="mailButton">'
+ '<img src="images/mail_Button.png" alt="mail" />'
+ '</div><!-- eof mail -->'
+ '<div id="" style="clear:both"></div>'
+ '<a href="#" onclick="deleteRecord('
+ item['id']
+ ')">delete</a></li>';
+'</div><!-- eof noteContainer -->';
console.log(i);
results.append(listing);
}
});
});
}
function createTable() {
db.transaction(function(tx) {
tx.executeSql(createStatement, [], showRecords, onError);
});
}
function insertRecord() {
db.transaction(function(tx) {
// consoleList();
console.log("The field:" + field);
tx.executeSql(insertStatement, [ dateDay, dateNum, dateTime, pumpBore, sInches, getMin,
maxEfficiency, field, percent ], loadAndReset, onError);
// ("Row Inserted!");
});
}
function loadRecord(i) {
var item = dataset.item(i);
dateDay.value = item['dateDay'];
dateNum.value = item['dateNum'];
dateTime.value = item['dateTime'];
pumpBore.value = item['pumpBore'];
sInches.value = item['sInches'];
sMin.value = item['sMin'];
maxEfficiency.value = item['maxEfficiency'];
field.value = item['field'];
percent.value = item['prodPercent'];
id.value = item['id'];
}
function updateRecord() {
db.transaction(function(tx) {
tx.executeSql(updateStatement, [ dateDay.value, dateNum.value,
dateTime.value, pumpBore.value, sInches.value, sMin.value,
maxEfficiency.value, field.value, percent.value ],
loadAndReset, onError);
});
}
function deleteRecord(id) {
db.transaction(function(tx) {
tx.executeSql(deleteStatement, [ id ], showRecords, onError);
});
resetForm();
}
function dropTable() {
db.transaction(function(tx) {
tx.executeSql(dropStatement, [], showRecords, onError);
});
resetForm();
}
function loadAndReset() {
// alert(listing);
resetForm();
showRecords();
}
function resetForm() {
dateDay.value = '';
dateNum.value = '';
dateTime.value = '';
pumpBore.value = '';
sInches.value = '';
sMin.value = '';
field.value = '';
percent.value = '';
id.value = '';
}
$(function() {
$('#slider-1').live('change', function(){
slider_value = $(this).val()
console.log(slider_value)
minChange = slider_value;
// minChange = 100 - minChange;
minChange = Math.round(minChange);
console.log("Min Change " + minChange);
calculate(minChange);
// do whatever you want with that value...
})
});
// / Knob Turn Function
$(function() {
$('#dialBG, #dialBG2 ').knobKnob({
snap : 1,
value : -10,
turn : function(ratio) {
console.log(ratio);
//minChange = (ratio * 100);
// minChange = 100 - minChange;
//minChange = Math.round(minChange);
//console.log("Min Change " + minChange);
//calculate(minChange);
}
});
});
}
我有一个链接到它kzoomarketing.com/apps/oilapp/121/所以可以看到。在计算器页面上,右上角的保存按钮记录所有字段,并在注释页面中填充它们。它在Web浏览器中工作正常。
如果我将js函数更改为ondeviceready并运行它,我会得到上面提到的log cat错误。
任何人都可以帮助我在Android设备上运行。
答案 0 :(得分:1)
insertRecord
函数在onDeviceReady
函数中定义。
您必须重新组织代码,以便像这样的事件处理程序可以全局访问(即从HTML)。