从文本输入插入Web SQL时出错



所以我仍在学习JavaScript,并且我正在处理的东西有问题。当试图在两个单独的文本输入中输入一些东西时,它只会默认为gotError函数,没有任何东西插入到Web SQL中,然后读回列表。

JavaScript:

function openDb() {
db = openDatabase('DName', '1', 'NameV', 2 * 1024 * 1024);
//(Database Name, Version, Display Name, Size )
db.transaction(function (tx) {   
tx.executeSql("CREATE TABLE IF NOT EXISTS Logs (id unique, person TEXT, place TEXT)"); 
});
}
document.addEventListener('init', function(event) {
if(event.target.id == 'mylist') {
openDb();
storeItems();
}
});
function gotError() {
alert('Something went wrong.');
}
function gotSuccess() {
storeItems()
}
function storeItems()
{
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM Logs", [], listItems, gotError);
});
}
function listItems(rs)
{
var output = '';
var list = document.getElementById('tList');
for(i = 0; i < rs.rows.length; i++)
{
stuff = person && place;
var row = rs.rows.stuff(i);
output += "ons-list-item>" + row.stuff +
"<div class="right"> <ons-button><ons-icon icon="trash"></ons-icon></ons-button></div>" +
"</ons-list-item>";
}
list.innerHTML = output;
}
function addItem() 
{
var textbox = document.getElementById("person", "place");
var value = textbox.value;
db.transaction(function(tx)
{
tx.executeSql("INSERT INTO Logs (person,place) VALUES (?,?)", [value], gotSuccess, gotError)
});
textbox.value = "";
fn.load("mylist.html");
}

HTML:

<template id="mylist.html">
<ons-page id='mylist'>
<ons-toolbar>
<div class="left">
<ons-toolbar-button onclick="fn.open()">
<ons-icon icon="md-menu"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center">
My List
</div>

</ons-toolbar>
<div>

<ons-input type="text" class="select-input--underbar" id="person" placeholder="Enter person here . . ."></ons-input>
<ons-input type="text" class="select-input--underbar" id="place" placeholder="Enter place here . . ."></ons-input>

<ons-button modifier="large" onclick="addItem()">Add Item</ons-button>

</div>




<ons-list id='tList'>
<ons-list-header>Listed Items:</ons-list-header>
<ons-list-item>
</ons-list-item>
</ons-list>


</ons-page>
</template>
</ons-page>
</template>

最初,代码应该插入两个需要填写的文本输入,然后按下一个按钮将它们添加到列表中,然后显示个人和地点的数据库内容。然后,每次打开或关闭页面时,它都会重新读取数据库的内容,并在列表中重新显示它们。

这是一个错误:

stuff = person && place;

因为:

  • 您正在将stuff声明为全局变量(strict模式中的错误(
  • personplace未声明

可能正在处理此错误catch',然后执行gotError。如果用一个参数声明函数,您可能会看到:

function gotError(error) {
console.error('Something went wrong:', error);
}

最新更新