【发布时间】:2019-01-30 03:25:10
【问题描述】:
我有一个简单的 jquery 移动页面,它应该在 indexedDb 中找到 id,找到它之后,如果该 id 记录存在,则更新其余信息。 (我插入了 10 条记录) 我找不到正确的方法来做到这一点。如果我使用下面的代码,我会插入空数据,或者我的第二个按钮没有响应 这是我的代码: js:
let updateCompId = $("#updateCompId").val();
$("#updateButton").click(function() {
// var updateCompId = $("#updateCompId").val();
let tx = db.transaction(["Clients"], 'readwrite');
let store = tx.objectStore("Clients");
var readCompanyId = store.get(updateCompId);
if (readCompanyId = updateCompId) {
store.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
document.getElementById('updateForm').style.display = "";
cursor.continue();
}
};
}
});
$("#updateInfo").click(function() {
console.log("here");
var tran = db.transaction(["Clients"], "readwrite");
var store = tran.objectStore("Clients");
// var compId = $("#updateCompId").val();
var updateCompName = $("#updateCompName").val();
var updatePost = $("#updatePost").val();
var updateCompAddress = $("#updateCompAddress").val();
var updateCity = $("#updateCity").val();
var updateCountry = $("#updateCountry").val();
var updateContact = $("#updateContact").val();
var updatePhone = $("#updatePhone").val();
var updateEmail = $("#updateEmail").val();
var item = {
id:updateCompId,
Name:updateCompName,
Post:updatePost,
Address:updateCompAddress,
City:updateCity,
Country:updateCountry,
Contact:updateContact,
Phone:updatePhone,
Email:updateEmail
};
console.log(item);
store.put(item);
});
HTML:
<form id="findId">
Enter Company Id to update information:
<input id="updateCompId" type="text" name="updateCompId"/>
<button id="updateButton" class="ui-btn">find to update</button>
<p id="updateCustomer"></p>
<button id="updateInfo" class="ui-btn">Update</button>
</form>
<form id="updateForm">
Company name:
<input id="updateCompName" type="text" name="compName"/>
Post code:
<input id="updatePost" type="text" name="post"/>
Company address:
<input id="updateCompAddress" type="text" name="compAddress"/>
City:
<input id="updateCity" type="text" name="city"/>
Country:
<input id="updateCountry" type="text" name="country"/>
Contact number:
<input id="updateContact" type="text" name="contact"/>
Company phone:
<input id="updatePhone" type="text" name="phone"/>
Email:
<input id="updateEmail" type="text" name="email"/>
</form>
【问题讨论】:
-
您是否为每条记录重复这些表格?
-
你明白
if (readCompanyId = updateCompId)这行在做什么吗?
标签: javascript jquery indexeddb