jQuery progressbar: progress based on user input where values get saved

天涯浪子 提交于 2020-01-06 15:43:05

问题


I am trying to implement a jQuery progressbar where the user determines the progress (by typing into a text form), but if the user were to leave the page and then come back to it, the progressbar wouldn't be back at 0 again; it would be wherever they left off from before. Trying to figure out how to save their progress....

<form method="post" action="">
  <input id="first-value" type="text" name="first" value="" /><br />
  <input type="text" name="second" value="" />         
</form> 

<script>
 $(function() {
  $( "#progressbar" ).progressbar({
    value: 0  //set to zero for now, but want it to change permanently when user    
             // inputs data
      });
});
</script>

<script>
   $('input').change(function(){
    var new_amount = 0
    $('input').each(function() {

      if($(this).val() == "50") {
             new_amount += 5;   //5 Percent
      }
      if($(this).val() == "100") {
             new_amount += 10;  //10 Percent
       }
     }
   );

$("#progressbar").progressbar('value',new_amount);

});
</script>

I want to take the values the user inputs (i.e. say, 100, 30, 80, etc.), add them up, make that amount the progressbar value, but keep it at that value even if they were to close the browser and come back to that page again.


回答1:


Generally, a cookie can be an effective way to store that information across pages/refreshes/back/forward/etc within the same site. It needs to be used judiciously though, and you should be careful of cases where the site seems to "cache" the cookie due to issues in your javascript code.

See W3c for a complete rundown on what they are and how to use them:

http://www.w3schools.com/js/js_cookies.asp

You could also store a session server side if that is more appealing. If that is the case, please let us know what server side technology you are using. (.NET, PHP, Java, Node.js, etc)



来源:https://stackoverflow.com/questions/14571852/jquery-progressbar-progress-based-on-user-input-where-values-get-saved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!