我有这段代码将复选框的选中值存储在 localStorage 中。
我该如何缩小它?
$('#OSPone').click(function(e){
saveSettings(e["currentTarget"]["id"], e["currentTarget"]["checked"]);
getSettings(e["currentTarget"]["id"]);
});
$("#OSPtwo").click(function(e){
saveSettings(e["currentTarget"]["id"], e["currentTarget"]["checked"]);
getSettings(e["currentTarget"]["id"]);
});
$("#OSPthree").click(function(e){
saveSettings(e["currentTarget"]["id"], e["currentTarget"]["checked"]);
getSettings(e["currentTarget"]["id"]);
});
$("#OSPfour").click(function(e){
saveSettings(e["currentTarget"]["id"], e["currentTarget"]["checked"]);
getSettings(e["currentTarget"]["id"]);
});
设置功能:
function saveSettings(aVal, aKey){
localStorage.setItem(aVal, JSON.stringify(aKey))
}
function getSettings(aKey){
var str = localStorage.getItem(aKey);
return JSON.parse(str);
}