我遇到的问题是复选框值不会改变。我错过了什么?帮助赞赏!
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);
});
答案 0 :(得分:2)
$(本).VAL();总是有价值的&#39; on&#39;。你应该使用
value = $(this).is(':checked') ? 'on' : 'off'
答案 1 :(得分:0)
可以value = $(this).is(':checked')
和localStorage.getItem(name) == "true"
$("input[type=checkbox]").each(function() {
var name = $(this).attr('name');
if (localStorage.getItem(name) == "true") {
$(this).prop('checked', true);
}
});
$("input[type=checkbox]").change(function() {
var name = $(this).attr('name'),
value = $(this).is(':checked');
localStorage.setItem(name, value);
});
&#13;