我是编程新手,我在JavaScript中使用cookies很费劲,所以我使用了本教程here。我使用了'创建cookie函数',但我不确定如何创建一个函数,将cookie放回页面加载的文本框中。我看过W3学校但仍然不知道。有什么想法吗?
这是我使用的创建cookie函数,它从'多个'文本框创建cookie。
function createCookie(nCookie){
var expDate = new Date();
expDate.setMonth(expDate.getMonth() + 12);
var cookieVal = document.getElementById(nCookie).value;
document.cookie = nCookie + "=" + cookieVal + ";path=/;expires=" +
expDate.toGMTString();}
答案 0 :(得分:0)
new loop
[[[1], 2, 3, 4], [[2], 1, 4], [[3], 1, 4], [[4], 2, 3]] # input_v
[[[1], 2, 3, 4], [[2], 1, 4], [[3], 1, 4], [[4], 2, 3]] # vertices
[[[1, 3], 2, 4], [[2], 1, 4], [[4], 2, 3]]
[[[1, 3, 2], 4], [[4], 2, 3]] # vertices at the end of Karger's
new loop
[[[1, 3, 2], 4], [[2], 1, 4], [[3], 1, 4], [[4], 2, 3]] # input_v (seems to be a hybrid)
[[[1, 3, 2], 4], [[2], 1, 4], [[3], 1, 4], [[4], 2, 3]] # vertices
[[[1, 3, 2], 4], [[2], 1, 4], [[3, 4], 1, 2]]
[[[1, 3, 2, 3, 4]], [[2], 1, 4]] # Garbage because of unexpected behaviour
答案 1 :(得分:0)
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h2>Working Example</h2>
<form name="myForm" method="get" action="JavaScriptCookies.html" onsubmit="return storeValues(this);">
<fieldset>
<label>Field 1</label><span><input type="text" name="field1" value=""></span>
<label>Field 2</label><span><input type="text" name="field2" value=""></span>
<label>Field 3</label><span><input type="text" name="field3" value=""></span>
<label>Field 4</label><span><input type="text" name="field4" value=""></span>
<span>
<input type="submit" value="Set Cookies">
<input type="button" onclick="showCookies();" value="Retrieve Cookies">
</span>
</fieldset>
</form>
<script type="text/javascript">
function storeValues(form)
{
document.cookie = 'field1=' + form.field1.value;
document.cookie = 'field2=' + form.field2.value;
document.cookie = 'field3=' + form.field3.value;
document.cookie = 'field4=' + form.field4.value;
return true;
}
function showCookies() {
for (var i = 1; i <= 4; i++) {
x = document.getElementsByName("field" + i);
x[0].value = getCookie(x[0].name);
}
return true;
}
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
</script>
</body>
</html>
答案 2 :(得分:-1)
http://www.w3schools.com/js/js_cookies.asp
BTW如果在欧盟,您需要声明cookie使用