PHP/[removed] Remembering state of form/form field

2019-06-04 20:16发布

问题:

In this question, I found out how to dynamically add form fields.

Now, how can I set a cookie to remember the amount of form fields that were generated, and their content?

eg. First visit, user types this:

Input one
Input two
Input three

Then he/she visits the page again. There are 3 form fields, containg Input one, Input two and Input 3.

Is this possible using a server/client side solution?

Thanks a lot,

Harley

回答1:

assuming that the input textfield class is "textfield1" -

This snippet will capture the data into a cookie when the value is modified

  $(".textfield1").change(function() {
       $.cookie("textfield1", $(".textfield1").val(), {expires: 7});
  });

This snippet will read the cookie data and populate the field on page load -

  $(".textfield1").val( $.cookie("textfield1") );

Put both these snippets inside $(document).ready(function(){ }); and you are good to go !



回答2:

there is a useful plugin of jquery "Jquery Populate" you can store your json in cookie/session and use it to fill your forms.