【问题标题】:Remember text box value using jQuery cookies使用 jQuery cookie 记住文本框的值
【发布时间】:2014-01-21 20:51:01
【问题描述】:

我的页面中有一个文本框和提交按钮。当我单击提交按钮时,页面会刷新。我想在刷新后记住最后输入到文本框中的值。我怎样才能使用 jQuery 做到这一点?我有以下

  <input type="text" id = "textbox"> <br/>
  <input type="Submit" value = " Submit " onClick = " javascript:window.location.reload(); ">

【问题讨论】:

    标签: jquery html cookies


    【解决方案1】:

    您可以使用local storage 解决问题

    localStorage.setItem('yourIndex',$('#textbox').val());
    

    现在如果你想检索项目

    localStorage.getItem('yourIndex');
    

    所有最新的浏览器都支持local storage

    【讨论】:

      【解决方案2】:

      保存cookie:

      $.cookie(key,value,{ expires: expire }); //expire in days.
      

      获取cookie:

      $.cookie(key);
      

      当然需要js,可以从:

      http://plugins.jquery.com/cookie/

      function OnSubmit (){
          $.cookie("textbox",$("#textbox").val(),{ expires: 1}); //stores cookie for 1 day, names textbox and stores the value
          window.location.reload();
      };
      
      $(document).ready(function() {
          var value = $.cookie("textbox");
          $("#textbox").val(value === undefined || value === null ? "" : value); //insert the value stored in cookie "textbox" only if its not null or undefined , if it does insert empty string
      });
      

      当然还有:onClick:OnSubmit

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        相关资源
        最近更新 更多