I am writing a set of functions in JavaScript where a user can either add a new trip or edit an existing trip. Each trip can have (0-n) guns associated to that trip. Either way, the save and edit function both call rangeSaveGun. When called from “save new trip” function, this function (below) executes perfectly. The new record is added to the DB and displayed. However, when called from “edit trip” function, the SQL is exactly the same as shown in alert(sql), but the record never saves to the DB. Any idea why? In both scenarios, this is only called to add new guns to the trip (not to edit existing guns).
My question: In the below function, why is the row not being inserted into the DB when this exact same function is called from the “Edit” function instead of the “New” function? Either way the “sql” text is exactly the same (as verified in the “alert”). What am I missing? Both the "new" and the "edit" functions are called via a button click, and simply save or edit the trip and then call this rangeSaveGun function at the end.
I am already using the standard workaround to ensure closure. I can confirm that all 3 variables (tripNum, input2temp, and input3temp) are showing the expected values in all scenarios. All values are numbers.
function rangeSaveGun(tripNum)
{
var tbl2 = document.getElementById("mytable2");
var rowCount=tbl2.rows.length;
for (var i = 1; i < rowCount; i++) {
var el = document.getElementById("GunVal"+i)
if(el != null)
{
(function(i){
var input2temp = document.getElementById("GunVal"+i).value;
var input3temp = document.getElementById("Rounds"+i).value;
var sql = 'INSERT INTO RangeShot (TripID, GunID, Rounds) VALUES ('+tripNum+', '+input2temp+', '+input3temp+')';
db.transaction(function (tx) {tx.executeSql(sql, [])});
alert(sql);
})(i);
}
}
}
Aucun commentaire:
Enregistrer un commentaire