简体   繁体   English

localStorage removeItem不适用于键值对

[英]localStorage removeItem not working for Key-value pair

any idea why my code doesn't remove the key-value pair from localStorage? 知道为什么我的代码没有从localStorage中删除键值对吗? If I console log rowItem it is precisely as written in localStorage. 如果我管理日志rowItem,则它与在localStorage中编写的完全一样。 If I hardcode the exact same string for example: localStorage.removeItem("Puppy") it is removed from localStorage. 如果我对相同的字符串进行硬编码,例如:localStorage.removeItem(“ Puppy”),则会将其从localStorage中删除。 Any advice? 有什么建议吗?

Link to app: http://kajlax.mbnet.fi/Projects/Kauppalista 链接到应用程序: http : //kajlax.mbnet.fi/Projects/Kauppalista

// Remove item
$("#shoppingTable > tbody").on("click", "i", (function(){
    let currentRow = $(this).closest("tr");
    let rowItem = currentRow.find("td:eq(0)").text().substring(1);

    // Delete from LS
    localStorage.removeItem(rowItem);

    // Delete from UI
    $(this).closest('tr').remove();
}));

Thank you Aamir Nakhwa for pointing out the error. 感谢Aamir Nakhwa指出错误。 It wasn't in this function but in another function in which I changed the selector: 它不是在此函数中,而是在另一个我更改了选择器的函数中:

$(".ui.container").on("click", "tr",(function(){

Both functions used a similar selector #shoppingTable and caused a collision. 这两个函数都使用了类似的选择器#shoppingTable并引起了冲突。

I have checked your demo link and want to make you clear that this line is working fine 我已经检查了您的演示链接,并希望使您清楚此行是否正常

localStorage.removeItem(rowItem); localStorage.removeItem(rowItem); All Good 都好

Actually after removing item, this event fired $("#shoppingTable > tbody").on("click", 实际上,在删除项目之后,此事件触发了$(“#shoppingTable> tbody”)。on(“ click”,

and it execute your else clause which add item again in localStorage by following line 然后执行您的else子句 ,该子句通过以下行再次在localStorage中添加项目

localStorage.setItem(stateOfItem, 0); localStorage.setItem(stateOfItem,0);

You can confirm it by log something from your else clause 您可以通过记录else子句中的内容进行确认

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM