PHP / Javascript:使用Cookie记住表单/表单字段的状态 [英] PHP/Javascript: Remembering state of form/form fields with cookies
问题描述
在此问题,我找到了如何动态添加表单字段。
In this question, I found out how to dynamically add form fields.
现在,如何设置一个cookie来记住生成的表单字段的数量及其内容?
Now, how can I set a cookie to remember the amount of form fields that were generated, and their content?
首次访问,用户输入:
Input one
Input two
Input three
然后他/她再次访问该页面。有3个表单字段,包含输入1,输入2和输入3。
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?
非常感谢,
Harley
推荐答案
假设输入textfield类是textfield1 -
assuming that the input textfield class is "textfield1" -
当值被修改时,此片段将捕获数据到cookie中
This snippet will capture the data into a cookie when the value is modified
$(".textfield1").change(function() {
$.cookie("textfield1", $(".textfield1").val(), {expires: 7});
});
此代码段将读取Cookie数据并填充页面加载的字段 -
This snippet will read the cookie data and populate the field on page load -
$(".textfield1").val( $.cookie("textfield1") );
将这两个片段放在$(document).ready(function(){});你很好!
Put both these snippets inside $(document).ready(function(){ }); and you are good to go !
这篇关于PHP / Javascript:使用Cookie记住表单/表单字段的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!