Javascript 从文本输入插入Web SQL时出错
因此,我仍然在学习JavaScript,并且对我正在研究的东西有一个问题。当尝试在两个单独的文本输入中输入内容时,它只会默认使用gotError函数,不会将任何内容插入Web SQL,然后读回列表 JavaScript:Javascript 从文本输入插入Web SQL时出错,javascript,html,web-sql,Javascript,Html,Web Sql,因此,我仍然在学习JavaScript,并且对我正在研究的东西有一个问题。当尝试在两个单独的文本输入中输入内容时,它只会默认使用gotError函数,不会将任何内容插入Web SQL,然后读回列表 JavaScript: function openDb() { db = openDatabase('DName', '1', 'NameV', 2 * 1024 * 1024); //(Database Name, Version, Display Na
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");
}
函数openDb(){
db=openDatabase('DName','1','NameV',2*1024*1024);
//(数据库名称、版本、显示名称、大小)
db.事务(功能(tx){
tx.executeSql(“创建不存在的表日志(id唯一、人员文本、位置文本)”);
});
}
document.addEventListener('init',函数(事件){
如果(event.target.id=='mylist'){
openDb();
storeItems();
}
});
函数gotError(){
警惕(‘出了什么事’);
}
函数gotsucture(){
storeItems()
}
函数storeItems()
{
数据库事务(功能(tx){
tx.executeSql(“从日志中选择*”,[],列表项,gotError);
});
}
功能列表项(rs)
{
var输出=“”;
var list=document.getElementById('tList');
对于(i=0;i<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>
我的名单
添加项
所列项目:
最初,代码应该插入两个需要填写的文本输入,按下按钮将它们添加到列表中,然后显示person和place的db内容。然后,每次打开或关闭页面时,它都会重新读取数据库的内容,并在列表中重新显示这些内容。这是一个错误:
stuff=人和地点;
因为:
- 您正在将
声明为全局变量(在stuff
模式下出错)strict
和person
未声明place
gotError
。您可能已经看到,如果使用参数声明函数:
函数错误(error){
console.error('出错:',error);
}
这是一个错误:
stuff=人和地点;
因为:
- 您正在将
声明为全局变量(在stuff
模式下出错)strict
和person
未声明place
gotError
。您可能已经看到,如果使用参数声明函数:
函数错误(error){
console.error('出错:',error);
}
Ah,这确实更有意义,但我想我的下一个问题是,我将如何进行for循环来呈现person和place的文本输入?您需要从DB接收到的每一行中获取这两个输入,类似于const{person,place}=rs.rows.item(I)代码>啊,这确实更有意义,但我想我的下一个问题是,我将如何进行for循环来呈现person和place的文本输入?您需要从DB接收到的每一行中获取这两个输入,并使用类似以下内容const{person,place}=rs.rows.item(I)代码>