gpt4 book ai didi

javascript - 本地存储迭代获取for循环内单击按钮的值

转载 作者:行者123 更新时间:2023-11-28 00:45:55 27 4
gpt4 key购买 nike

我有这个代码:

for(var i = 0; i < localStorage.length; i++){
var dataNya = JSON.parse(localStorage.key(i));
// console.log(dataNya);
var displayUlit = "";
displayUlit += "<br/>";
var spaceTo = "";
spaceTo += " || ";
console.log(dataNya);
var keme = localStorage.getItem(localStorage.key(i));
$("#pangDisplay").append(dataNya + spaceTo + "<button class='pangEdit' value="+dataNya+">"+JSON.parse(keme)+"</button>" + " " + displayUlit);
console.log(JSON.parse(keme));
// localStorage.setItem(JSON.stringify(dataNya), JSON.stringify("complete"));
}
$(".pangEdit").click(function(e){
e.preventDefault();

var pangInput = $(".pangEdit").val();
console.log(pangInput);
localStorage.setItem(JSON.stringify(pangInput), JSON.stringify("complete"));
});

我想获取本地存储的 key ,我可以在其中传递数据并更新其值。帮帮我谢谢

最佳答案

您可以在 jQuery 中使用附加的 data 属性来实现此目的:

for (var i = 0; i < localStorage.length; i++) {
var dataNya = localStorage.key(i);

var displayUlit = "";
displayUlit += "<br/>";

var spaceTo = "";
spaceTo += " || ";

var keme = localStorage.getItem(dataNya);
var button = "<button class='pangEdit' data-storagekey='" + dataNya + "' value='" + dataNya + "'>" + keme + "</button>";
$("#pangDisplay").append(dataNya + spaceTo + button + " " + displayUlit);
}

$(".pangEdit").click(function(e) {
e.preventDefault();

var dataNya = $(this).data('storagekey');

localStorage.setItem(dataNya, "complete");
});

关于javascript - 本地存储迭代获取for循环内单击按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27398063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com