SQLite:SELECT * FROM Tble 的问题在哪里?
我正在使用 SQLite、Javascript 和 Chrome 离线工作 在我的主页(main.html)中,我有两个 div:
<div id="content">
以及item 的编写如下:
<li>ItemName1
<div id="idItem1" class="editItem_btn">
<img src="btn_edit.png">`
</div>
</li>
在 main.html 中,我有以下代码:
$("#menuLeft").delegate(".editItem_btn", "click", function(e0)
{
e0.preventDefault();
var editItemId = $(this).attr("id");
editItemId = parseInt(editItemId);
var url="edititem.html"
$("#content").load(url,function(){
loadRecord(editItemId);`
});
});
当我单击给定 Item 的编辑按钮时,首先从编辑按钮周围的 div 的 id 中检索该 Item 的 id。然后我加载页面 edititem.html 内容。成功后,我运行函数 loadRecord(editItemId),其中 loadRecord(i) 包含在 edititem.html 中:
function loadRecord(j)
{
var item = dataset.item(j);
idItem.value = item['id'];
ItemName.value = item['ItemName'];
dateStart.value = item['dateStart'];
dateEnd.value = item['dateEnd'];
notes.value = item['notes'];
}
该函数可以显示数据库中包含的 Item (id, ItemName...) 的参数。
这是我的问题,代码可以工作,但以一种奇怪的方式意味着如果我单击 Item1 的编辑按钮,则会显示 Item2 的参数。同样,如果我单击编辑 Item2,则会显示 Item3 的参数。
然后我将: 替换
var item = dataset.item(j);
为:
var item = dataset.item(j-1);
并且有效。但我需要理解为什么它会这样,以及为什么我需要使用 (j-1)。我在 jquery 代码中放置了一些alert(),以检查我是否拥有正确的 editItemId 编号,并在函数 loadRecord(j) 中放置了一些alert()。单击后会检索正确的 ID 号,并将正确的 ID 号传递给该函数。我不知道这里有什么错误!
I am working offline with SQLite, Javascript and Chrome
In my main page (main.html), I have two div: <div id="menuLeft">
that contains the list of items name with buttons to edit each item, and
<div id="content">
The list of item is written as follows:
<li>ItemName1
<div id="idItem1" class="editItem_btn">
<img src="btn_edit.png">`
</div>
</li>
In main.html, I have the following code:
$("#menuLeft").delegate(".editItem_btn", "click", function(e0)
{
e0.preventDefault();
var editItemId = $(this).attr("id");
editItemId = parseInt(editItemId);
var url="edititem.html"
$("#content").load(url,function(){
loadRecord(editItemId);`
});
});
When I click on the edit button of a given Item, the id of the Item is first retrieved from the id of the div around the edit button. Then I load the page edititem.html content. On success, I run the function loadRecord(editItemId), where loadRecord(i) is contained in edititem.html:
function loadRecord(j)
{
var item = dataset.item(j);
idItem.value = item['id'];
ItemName.value = item['ItemName'];
dateStart.value = item['dateStart'];
dateEnd.value = item['dateEnd'];
notes.value = item['notes'];
}
This function enables to display the parameters of Item (id, ItemName....) contained in the database.
Here is my problem, the code works but in a weird way meaning that if I click on the edit button of Item1, the parameters of Item2 are displayed. Same thing if I click on edit Item2, parameters of Item3 are displayed.
I then replaced:
var item = dataset.item(j);
with:
var item = dataset.item(j-1);
and that works. But I need to understand why it's behaving like that, and why I need to use (j-1). I placed some alert() in the jquery code to check that I have the right editItemId number, and in the function loadRecord(j). The right id number is retrieved after the click and the right id number is passed to the function. I have no idea what's the bug here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有看到 sql 方面的内容,以及如何将数据传递回脚本,就不可能准确地告诉您发生了什么,但这只是一些列表基于 0 和一些列表基于 1 的情况。例如,数组通常是基于 0 的(除非您专门以不同的方式创建它们),但 $("#id").each(function(Index)... 是基于 1 的。您只需要知道您是什么使用并偶尔按照您发现的方式进行操作,并在相关时使用 -1 或 +1。
Without seeing the sql side of things, and how that data is passed back to your script it's impossible to tell you exactly what's happening, but this is simply a case of some lists being 0 based and some lists being 1 based. For example, arrays are generally 0 based (unless you specifically create them a different way), but $("#id").each(function(Index)... is 1 based. You just have to know what you're working with and occasionally do as you have found and use -1 or +1 when relevant.
虽然我不熟悉 SQLite 的复杂性,但我怀疑
dataset.item(j)
:我会检查是否有一个等效的
dataset.item(j)
接受 record_id 而不是索引。否则,您可能希望将记录的索引存储在记录本身的某个位置,以便能够将其传递给loadRecord
函数。希望这有帮助,
皮特
While I'm not familiar with the intricacies of SQLite, I suspect that
dataset.item(j)
:I would check to see if there's an equivalent for
dataset.item(j)
which accepts a record_id and not an index. Otherwise, you'll probably want to store the index of the record somewhere in the record itself to be able to pass it to yourloadRecord
function.Hope this helps,
Pete