2014-01-19 4 views
0

나는 어떻게 든 다음 코드와 함께 텍스트 필드 id를 사용하여 텍스트 필드의 내용을 삽입 할 수 있었지만 텍스트 필드의 내용을 변수에 할당 한 다음 해당 변수의 값을 삽입했습니다.SQLite에서 데이터베이스에 변수 값을 삽입하려면 어떻게해야합니까?

var newName = document.getElementById("txNewName").value; 
var ing1 = document.getElementById("txIngredient1").value; 
var ing2 = document.getElementById("txIngredient2").value; 
var ing3 = document.getElementById("txIngredient3").value; 

db.transaction(function(transaction) { 
    transaction.executeSql('INSERT INTO MyRecipes(MyRecipeName, MyIngredient1, MyIngredient2, MyIngredient3) VALUES (?,?,?,?)',[$('#txNewName').val(), $('#txIngredient1').val(), $('#txIngredient2').val(), $('#txIngredient3').val()]); 
}); 

차이점이 있으며 매우 새로운 점이 있다면 어떤 도움을 주시면 SQLite를 사용하고 있습니다.

+0

를 제외하고, 당신은 자바 스크립트와 jQuery를 믹싱 - 더 나은 JQuery와 구문 당신이 그것을 사용하는 경우에 충실. – Turophile

답변

0

주어진 코드에서 변수는 다른 범위에서 선언되므로 함수 내에서 값을 알 수 없습니다. 전역 변수를 사용하거나 매개 변수로 전달해야합니다. 아래에서는 함수를 함수 안에 넣기위한 대답을 변경했습니다. 이것은 당신이 원하는되지 않을 수도 있습니다,하지만 문제는 여기서 그것을 확인합니다 : 다른로서

db.transaction(function(transaction) { 
    var newName = document.getElementById("txNewName").value; 
    var ing1 = document.getElementById("txIngredient1").value; 
    var ing2 = document.getElementById("txIngredient2").value; 
    var ing3 = document.getElementById("txIngredient3").value; 
console.log('Inserting into the database ' + newName + ',' + ing1 +',' + ing2 + ',' + ing3); 
    transaction.executeSql('INSERT INTO MyRecipes(MyRecipeName, MyIngredient1, MyIngredient2, MyIngredient3) VALUES (?,?,?,?)',[newName, ing1, ing2, ing3]); 
}); 
+0

예, 데이터베이스가 'undefined undefined undefined undefined'를 표시합니다. 시도한 대부분의 메서드와 같습니다. 생각해 줘서 고마워. – Marblex

+0

예, 주어진 코드에서 변수가 다른 범위로 선언되었으므로 함수 내에서 값을 알 수 없습니다. 전역 변수를 사용하거나 매개 변수로 전달해야합니다. 나는 함수를 함수 안에 넣기위한 대답을 바꾸었다. 이것은 원하는 것이 아닐 수도 있지만, 문제가있는 곳을 확인할 것입니다. – Turophile

+0

물론 감사합니다.하지만 전역 변수를 전역 변수로 만들고 매개 변수로 전달하려고 시도했지만 데이터베이스에 'undefined undefined undefined undefined'가 표시됩니다. – Marblex

관련 문제