0
我在這裏工作的一項任務,用戶可以在文本框中輸入他們的名字並選擇帶有單選按鈕的圖片,驗證並將值存儲在cookie中。問題獲取cookie值
儘管它沒有完美執行,但我看到cookie正在通過打印document.cookie
正確存儲。我現在想要做的只是顯示值而不是cookie名稱。目前爲止,沒有任何東西顯示給我。我懷疑這個問題出現在我的GetCookie
函數或其被調用的地方。
任何指針,將不勝感激!
// Functions
$("#BtnSubmit").click(function() {
ClearErrors();
Validate();
});
$("#BtnShow").click(function() {
GetCookie(document.cookie);
});
$("#BtnDestroy").click(function() {
DestroyCookie();
});
function Validate() {
var name = $("#TBName").val();
var pic = $("input:radio[name='RGPic']:checked").val();
var isValid = true;
// validate name field
var nameReg = /^[A-Za-z ]{3,20}$/;
if (!nameReg.test(name) || name.length == 0) {
$("#DivNameError").html("C'mon, \"" + name + "\" is not a valid name.");
isValid = false;
}
if (name.length == 0) {
$("#DivNameError").html("No name was entered!");
isValid = false;
}
// Validate picture selected
if (pic == undefined) {
$("#DivPicError").html("Select a picture");
isValid = false;
}
if (isValid == true) {
SetCookie(name, pic);
}
}
// function to clear error messages upon attempted submittal
function ClearErrors() {
$("#DivNameError").html("");
$("#DivPicError").html("");
}
// Cookie Functions
function SetCookie(nValue, picture) {
document.cookie = "name=" + nValue + "; expires=Wed, 01 Jan 2020 00:00:00 GMT; path=/";
document.cookie = "securitypicture=" + picture + "; expires=Wed, 01 Jan 2020 00:00:00 GMT; path=/";
$("#DivMessage").show().html("Cookie has been added.").fadeOut(1600);
window.location.href = "results.html";
}
function GetCookie(list) {
var cookieArr = new array();
cookieArr = list.split(";");
for (var i = 0; i < cookieArr.length; i++) {
var val = cookieArr[i].split("=");
}
$("#DivCookieContents").html("name value: " + val[0] + "picture file: " + val[1]);
}
function DestroyCookie() {
var name = "";
var pic = "";
SetCookie(name, pic);
}
'GetCookie'只顯示最後一塊餅乾。你應該在循環內附加你的HTML。 – Barmar 2014-11-02 03:30:25