Tuesday, 15 May 2012

javascript - I got a annoying bug in storing input value using localStorage -



javascript - I got a annoying bug in storing input value using localStorage -

i want store current inputs if user refresh or close browser.

my problem if click yes in radio button , refresh page or close browser , reopen no button checked , yes button unchecked.

testing link: http://jsfiddle.net/5kcsn/124/

current script:

$(document).ready(function () { $('#employed_v1, #employed_v0, #test').on("change", function () { debugger; localstorage.setitem($(this).attr("id"), $(this).val()) }); $('#employed_v1, #employed_v0, #test').each(function (ind, val) { debugger; if ($(val).attr("id") != "employed_v1") { $(val).val(localstorage.getitem($(val).attr("id"))) } if ($(val).attr("id") != "employed_v0") { $(val).val(localstorage.getitem($(val).attr("id"))) } else { if (localstorage.getitem($(val).attr("id")) == "yes"){ $("#employed_v1[value=yes]").prop("checked", true);} if (localstorage.getitem($(val).attr("id")) == "no"){ $("#employed_v0[value=no]").prop("checked", true);} } }); $('[id="employed_v0"]').on('click', function(){ $('#test').val(''); $('#test').prop('disabled', true); }); $('[id="employed_v1"]').on('click', function(){ $('#test').prop('disabled', false); }); });

to store check state should utilize checked attribute. however, here modified version :

$(document).ready(function () { $('#employed_v1, #employed_v0').on("change", function () { localstorage.setitem('employed', $(this).attr("id")) }); $('#' + localstorage.getitem('employed')).attr('checked', true); });

javascript jquery html html5 local-storage

No comments:

Post a Comment