javascript - How to use localStorage to remain disable an input even if refreshed -
this question has reply here:
how remember whether box checked or unchecked (on click) localstorage , save value? 2 answersi want if click no on radio button disable if refreshed or closed browser , reopen 1 time again still disable until click yes button disable gone.
how using localstorage?
testing link: http://jsfiddle.net/5kcsn/156/
html:
<input type="radio" name="employed" id="employed_v1" value="yes" required="required" /> yes<br /> <input type="radio" name="employed" id="employed_v0" value="no" required="required" /> no <br> <input type="text" name="test" id="test" /> script:
$(document).ready(function () { $('#test').on("keyup change", function () { debugger; localstorage.setitem($(this).attr("id"), $(this).val()) }); $('#test').each(function (ind, val) { debugger; $(val).val(localstorage.getitem($(val).attr("id"))) }); $('#employed_v1, #employed_v0').on("change", function () { localstorage.setitem('employed', $(this).attr("id")) }); $('#' + localstorage.getitem('employed')).attr('checked', true); $('[id="employed_v0"]').on('click', function(){ $('#test').val(''); localstorage.removeitem('test'); $('#test').prop('disabled', true); }); $('[id="employed_v1"]').on('click', function(){ $('#test').prop('disabled', false); }); });
try next code, check demo, alter radio button , come in text in textbox , run jsfiddle 1 time again remain in same state.
demo
$(document).ready(function(){ if(localstorage.getitem("itmemployee") == "yes") $("#employed_v1").prop("checked", true); else $("#employed_v0").prop("checked", true); if(localstorage.getitem("itmemployee") == "yes") $("#test").prop("disabled", false); else $("#test").prop("disabled", true); $("#test").val(localstorage.getitem("itmtest")); $("[name=employed]").on("change", function(){ localstorage.setitem("itmemployee", $(this).val()); if($(this).val() == "yes") $("#test").prop("disabled", false); else $("#test").prop("disabled", true); }); $("#test").on("change", function(){ localstorage.setitem("itmtest", $(this).val()); }); }); javascript jquery html html5 local-storage
No comments:
Post a Comment