javascript - Remember check box value when re-visited the same page - Stack Overflow

I have 3 check boxes in my page.FIDDLE <form><input type="checkbox" name="veh

I have 3 check boxes in my page. FIDDLE

<form>
    <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
    <input type="checkbox" name="vehicle" value="Car">I have a car <br>
    <input type="checkbox" name="vehicle" value="Scooter">I have a Scooter <br>
    <input type="submit" value=" Submit ">
</form>

When the user clicks on "Submit", it navigates to another page. When I visit the same page again, I want to see the values which I previously selected. How can I do that using Javascript?

I have 3 check boxes in my page. FIDDLE

<form>
    <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
    <input type="checkbox" name="vehicle" value="Car">I have a car <br>
    <input type="checkbox" name="vehicle" value="Scooter">I have a Scooter <br>
    <input type="submit" value=" Submit ">
</form>

When the user clicks on "Submit", it navigates to another page. When I visit the same page again, I want to see the values which I previously selected. How can I do that using Javascript?

Share Improve this question edited Jan 20, 2014 at 9:20 Anton 32.6k5 gold badges46 silver badges54 bronze badges asked Jan 20, 2014 at 9:09 Anusha MallajosyulaAnusha Mallajosyula 771 gold badge2 silver badges6 bronze badges 1
  • 1 you need to store the value some where... in client side using cookie/web storage or in server side – Arun P Johny Commented Jan 20, 2014 at 9:10
Add a ment  | 

3 Answers 3

Reset to default 2

You need to use a cookie (or some other persistent storage) to store the values of whatever is checked.

With a cookie, you can do this by attaching event listeners to the checkboxes, so that when they are updated (selected/deselected) a cookie is stored (or updated) containing their current values.

When the page initially loads, you will then check for the presence of the cookie and populate the boxes accordingly.

Here's one way you might do it. This uses the jQuery cookie library.

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Persist checkboxes</title>
  </head>

  <body>
    <div>
      <label for="checkAll">Check all</label>
      <input type="checkbox" id="checkAll">
    </div>
    <div>
      <label for="option1">Option 1</label>
      <input type="checkbox" id="option1">
    </div>
    <div>
      <label for="option2">Option 2</label>
      <input type="checkbox" id="option2">
    </div>
    <div>
      <label for="option3">Option 3</label>
      <input type="checkbox" id="option3">
    </div>

    <script src="http://ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="http://cdn.jsdelivr/jquery.cookie/1.4.0/jquery.cookie.min.js"></script>

    <script>
      $("#checkAll").on("change", function() {
        $(':checkbox').not(this).prop('checked', this.checked);
      });

      $(":checkbox").on("change", function(){
        var checkboxValues = {};
        $(":checkbox").each(function(){
          checkboxValues[this.id] = this.checked;
        });
        $.cookie('checkboxValues', checkboxValues, { expires: 7, path: '/' })
      });

      function repopulateCheckboxes(){
        var checkboxValues = $.cookie('checkboxValues');
        if(checkboxValues){
          Object.keys(checkboxValues).forEach(function(element) {
            var checked = checkboxValues[element];
            $("#" + element).prop('checked', checked);
          });
        }
      }

      $.cookie.json = true;
      repopulateCheckboxes();
    </script>
  </body>
</html>

Save it to cookie using document.cookie as key=value pairs separated by semicolons:

document.cookie = "vehicle=Bike; name=Billy;"

Then look it up when you build the page

If it is within the same browser session you can maintain them in sessions.

OR

Set a cookie to remember the selected value

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744326484a4568673.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信