我遇到的問題是複選框值不會更改。我錯過了什麼?幫助讚賞!將多個複選框的值保存到localStorage中
HTML:
<input type="checkbox" name="smth1" />something1<br />
<input type="checkbox" name="smth2" />something2<br />
<input type="checkbox" name="smth3" />something3<br />
<br />
<input type="checkbox" name="anthr1" />anotherthing1<br />
<input type="checkbox" name="anthr2" />anotherthing2<br />
<input type="checkbox" name="anthr3" />anotherthing3<br />
<br />
<input type="checkbox" name="new1" />newthing1<br />
<input type="checkbox" name="new2" />newthing2<br />
<input type="checkbox" name="new3" />newthing3<br />
etc etc
腳本:
$("input[type=checkbox]").each(function() {
var name = $(this).attr('name');
if (localStorage.getItem(name) == "on") {
$(this).prop('checked', true);
}
});
$("input[type=checkbox]").change(function() {
var name = $(this).attr('name'),
value = $(this).val();
localStorage.setItem(name,value);
});
變化'值= $(本).VAL()''到值= this.checked ? 'on':''' – Azim
感謝此作品 – Jim34