使用jquery的三个文本框的Cookie

时间:2014-01-22 08:43:06

标签: jquery cookies

我的页面中有3个文本框。我想在刷新后保留以前的值。我有以下内容。问题是,即使我输入三个不同的值,所有文本框在刷新后都具有相同的值(第一个)。我哪里错了?

 $(document).ready(function () {
      $(".companyname").val($.cookie("companyname")||'');
      $(".companyname1").val($.cookie("companyname1")||'');
      $(".companyname2").val($.cookie("companyname2")||'');
      $('#submit').click(function(){
          var value = $('.companyname').val();
          $.cookie('companyname', value);
          var value1 = $('.companyname1').val();
          $.cookie('companyname1', value);
          var value2 = $('.companyname2').val();
          $.cookie('companyname2', value);
          window.location.reload();
      });
 });

这些是我的元素

  <input type="text" name = "companyname" class = "companyname"> <br/>
  <input type="text" name = "companyname1" class = "companyname1"> <br/>
  <input type="text" name = "companyname2" class = "companyname2"> <br/>
  <input type="Submit" value = " Submit " id="submit">

1 个答案:

答案 0 :(得分:1)

忽视了这条线

 $.cookie('companyname', value);

以下是更正后的代码:

     $(document).ready(function () {
      $(".companyname").val($.cookie("companyname")||'');
      $(".companyname1").val($.cookie("companyname1")||'');
      $(".companyname2").val($.cookie("companyname2")||'');
      $('#submit').click(function(){
          var value = $('.companyname').val();
          $.cookie('companyname', value);
          var value1 = $('.companyname1').val();
          $.cookie('companyname1', value1);
          var value2 = $('.companyname2').val();
          $.cookie('companyname2', value2);
          window.location.reload();
      });
    });