私は簡単なタスクリストを書いた。以下のJavaScriptコードは重要な部分はlocalStorageです。私がこれまでに作ったのはこれです:JSBin保存された変数と同じ型がlocalStorageから取得されていませんか?
私が達成したいのは、ページをリロードするときに、エントリをすぐに削除する必要がある場合(テキストフィールドの横のチェックボックスがオンになっているかどうか)です。最後の訪問から保存され復元されました。私は最初、私はオフにして、その後、私はそれをしたいとそれを動作させるために、もう一度チェックボックスをチェックする必要があるページを読み込ん現在
...ここ
である私はJavaScript/jQueryのコード:
var anzahl = 0;
var autoremove = true;
var autoremove_backup = localStorage.getItem("autoremove");
console.log(localStorage.getItem("autoremove"));
$(document).ready(function() {
if(autoremove_backup===false){
$("#autoremove").prop("checked", false);
}
else if (autoremove_backup===true){
$("#autoremove").prop("checked", true);
}
autoremove = autoremove_backup;
setInterval(entry, 2000);
$("button").on('click', function() {
if(this.id=="add"){
var r = $('<div id="'+ "div"+String(anzahl) +'"><input type="checkbox" id="'+String(anzahl)+'">' + '<label for="'+ String(anzahl)+'" id="'+ "label" +String(anzahl)+'">' + $("#task").val() + '</label><br></div>');
$("#var").append(r);
anzahl = anzahl +1;
}
});
$('input[type=checkbox]').change(
function(){
if (this.checked) {
if(String(this.id)==="autoremove"){
autoremove=true;
saveAutoremove(autoremove);
}
}
else {
if(String(this.id)==="autoremove"){
autoremove=false;
saveAutoremove(autoremove);
}
}
});
});
function entry(){
if(autoremove===true){
$('#var input:checked').each(function() {
$("#div"+String(this.id)).remove();
});
}
}
function saveAutoremove(input){
localStorage.setItem("autoremove", input);
}
EVERYTHINGは 'localStorage'に**文字列**として格納されます。 – Tushar
ああ、ありがとう、私はそれがいくつかの型の問題かもしれないと思った... – frankenapps
文字列リテラルに何かを連結するとき、明示的に文字列にキャストする必要はありません、それは暗黙のうちにキャストされます。 '" #div "+ String(this.id)'の代わりに '' #div "+ this.id'を書くことができます。明確にするために明示的にしたいときもありますが、左にリテラルがあるときはかなり自明です。 –