2014-06-05 3 views
1

모든 버튼을 클릭 할 때마다 값을 10 씩 증가시키는 코드가 있습니다. 페이지를 새로 고칠 때마다 코드가 값을 증가 시키길 원합니다. 어떻게해야합니까? 에이 질문에 명시된 바와 같이웹 페이지에서 변수의 변수 값 증가시키기

history.back(); 

: 이전 페이지를 표시하기 위해 다음과 같은 자바 스크립트 기능을 사용할 수 있습니다, 귀하의 질문의 제목

<html> 
<head> 
<script type="text/javascript"> 
function reveal() { 
var val = document.getElementById("outputtext").value; 
var result = parseInt(val) + parseInt(10); 
document.getElementById("outputtext").value = result; 
} 
</script> 
</head> 

<body> 
<p> <h3>Click on below button to increase the value by 10<h3> </p> 

<button onclick="reveal()"> Increment</button> 

<table> 
<tr> 
<td><textarea id="outputtext">10</textarea></td> 
</tr></table> 
</body> 
</html> 
+3

제목과 질문에 상관 관계가 없습니다. – ndugger

+0

지금 수정 했습니까? 이제 이해가 되나요? – goal4321

답변

-1

당신은 사용하여 쿠키없이이 작업을 수행 할 수있는 전자는 사용자의 브라우저에 쿠키로이 변수를 저장해야합니다, 자바 스크립트로 새로 고침 URL의 해시 (#value). 코드가 실행됩니다 때 #outputtext가 이미 정의되어 있도록 내가, 파일의 마지막에 자바 스크립트를 이동

<html> 
<head> 
</head> 

<body> 
    <h3>Click on below button to increase the value by 10<h3> 

    <button onclick="reveal()"> Increment</button> 

    <table> 
     <tr><td><textarea id="outputtext">10</textarea></td></tr> 
    </table> 

    <script type="text/javascript"> 

    //Get the last value 
    var current = parseInt(location.hash.slice(1)); 
    //If it is set, increment it 
    if(current){ 
     document.getElementById("outputtext").value = current + 10; 
     location.hash = '#' + (current+10); 
    } 

    function reveal() { 
     var val = document.getElementById("outputtext").value; 
     var result = parseInt(val) + parseInt(10); 
     document.getElementById("outputtext").value = result; 
     //Set the value 
     location.hash = '#' + result; 
    } 

    </script> 
</body> 
</html> 

참고 :이 코드를 사용해보십시오.

+1

네, 그게 효과가 있습니다. 나는 [Spaghetti code] (http://en.wikipedia.org/wiki/Spaghetti_code)와 혼란의 원인이 될 수 있기 때문에 해시 값에서 멀리 떨어져 있습니다. – HelpingHand

+0

@HelpingHand 사실, 쿠키를 사용하는 것을 권장하는 큰 프로젝트에서이 방법을 사용하는 것이 좋습니다. – blex