2014-06-24 4 views
0

라디오 버튼 No을 클릭하면 브라우저를 새로 고치거나 닫은 다음 다시 열어도 비활성화됩니다. Yes 버튼을 누르면 비활성화됩니다.localStorage를 사용하여 새로 고침을해도 입력을 비활성화하는 방법

localStorage를 사용하는 방법?

테스트 링크 : 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" /> 

스크립트 코드를 다음

$(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); 
    }); 

}); 
+0

@CDspace는이 찾고있는 메신저 아무 대답이 없다. – user3763580

답변

0

시도, 데모, 변경 라디오 버튼을 체크하고 텍스트 상자에 텍스트를 입력하고 jsfiddle을 실행 다시 같은 상태로 남아있을 것입니다.

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()); 
    }); 
}); 
+0

No 버튼을 클릭하면 비활성화해야합니다. – user3763580

+0

@ user3763580 코드가 pl 체크 됨 –

+0

이 itmEmployee 및 itmTest는 어디에서 얻습니까? – user3763580

관련 문제