2014-10-09 8 views
1

양식이 있으며 입력란에 쿠키를 설정했습니다. 내가 inspect element로 갈 때 resources에있는 쿠키를 찾을 수 있습니다. 코드는 기밀이므로 쿠키를 설정하는 방법을 보여줄 수 없습니다. 그러나 나는 선택된 입력 필드를 resources-> cookie에서 찾을 수있을 것이라고 확신한다.입력란에 쿠키 값을 가져 오는 방법은 무엇입니까?

모든 페이지에 동일한 양식이 나타납니다. 한 페이지에서 다른 페이지로 리디렉션하면 선택한 양식 필드가 모든 페이지에 나타나야합니다.

나는 쿠키 값

<script type="text/javascript"> 
    $(document).ready(function() { 
     if(input1 = getCookie("input1 ")) 
      document.myform.input1.value = input1 ; 
    }); 
</script> 

하지만 Uncaught ReferenceError: getCookie is not defined

사람이 오류의 원인 일 것입니다 무슨 제안 할 수있는 오류를 얻고을 얻기를위한 아래의 코드를 사용? 어떻게 쿠키 입력 값을 입력 필드에 가져 옵니까?

+0

가능한 중복 도움이 될 희망 확인 자세한 내용은 바이올린 http://jsfiddle.net/hr4mubsw/5/

입니다 1599287/create-read-and-erase-cookies-with-jquery –

+0

이 사이트에서 사용 된 것과 거의 동일한 기능 http://www.w3schools.com/js/js_cookies.asp – Ghost

답변

0

일반적으로 getCookie와 같은 함수를 선언하면 쿠키를 설정하는 방법을 Google에 알려 주어야합니까?

<script type="text/javascript"> 
    function setCookie(key, value) { 
     var expires = new Date(); 
     expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000)); 
     document.cookie = key + '=' + value + ';expires=' + expires.toUTCString(); 
    } 

    function getCookie(key) { 
     var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)'); 
     return keyValue ? keyValue[2] : null; 
    } 

    $(document).ready(function() { 
     setCookie("input1",'1'); 
     alert(getCookie("input1")); 
     document.myform.input1.value = getCookie("input1"); 
    }); 

</script> 

그리고 여기이 하나 How do I set/unset cookie with jQuery?

그것이 http://stackoverflow.com/questions/의 :

+0

여기서 '키'를 얻으실 수 있습니다. 함수에서 getCookie (key) –

+0

실제로 키는 생성 된 쿠키 이름이 무엇이든지간에 매개 변수가 무엇인지에 대한 것입니다. 편집 된 ans를 보면 편집 방법을 알 수 있습니다. 위의 코드를 실행하면 작동합니다. –