使用表单值创建的cookie为空cookie



首先,我需要创建一个cookie的值是在一个形式,和很多值是在一个无线电输入类型,但我遇到了一个问题,创建它。

代码如下:

<script>
function setCookie(name, value, daysToLive) {
var cookie = name + "=" + encodeURIComponent(value);

if(typeof daysToLive === "number") {
cookie += "; max-age=" + (daysToLive*24*60*60);

document.cookie = cookie;
}
}
function getCookie(name) {
// Split cookie string and get all individual name=value pairs in an array
var cookieArr = document.cookie.split(";");

// Loop through the array elements
for(var i = 0; i < cookieArr.length; i++) {
var cookiePair = cookieArr[i].split("=");

/* Removing whitespace at the beginning of the cookie name
and compare it with the given string */
if(name == cookiePair[0].trim()) {
// Decode the cookie value and return
return decodeURIComponent(cookiePair[1]);
}
}

// Return null if not found
return null;
}
function checkCookie() {
// Get cookie using our custom function
var firstName = getCookie("firstName");

if(firstName != "") {
alert("Welcome again, " + firstName);
} else {
firstName = prompt("Please enter your first name:");
if(firstName != "" && firstName != null) {
// Set cookie using our custom function
setCookie("firstName", firstName, 30);
}
}
}
function createCookie() { 
if (!document.f1.txt1.value) {
alert("Имя не введено");
document.f1.txt1.focus();
}
else { 
name=document.f1.txt1.value;
value=document.f1.gender.value+","+document.f1.edu.value+","+document.f1.theme.value;
setCookie(name,value,4);
checkCookie();
}
}

我从tutorialrepublic.com得到了所有函数的代码,除了最后一个,这是我自己做的。它会根据我在表单中选择的值创建一个cookie当我点击这里的按钮

<form name="f1">
<p>Имя: <input type="text" name="txt1">
<p>Пол: <input type="radio" id="male" name="gender" value="male" checked>
<label for="male">Мужской</label> <br>
<input style="margin-left: 215e-2%;" type="radio" id="female" name="gender" value="female">
<label for="female">Женский</label>
<p>Образование: <input type="radio" id="elem" name="edu" value="elem" checked>
<label for="elem">Начальное</label> <br>
<input style="margin-left: 53E-1%;" type="radio" id="sec" name="edu" value="sec">
<label for="sec">Среднее</label><br>
<input style="margin-left: 53E-1%;" type="radio" id="high" name="edu" value="high">
<label for="high">Высшее</label>
<p>Цветок: <input type="radio" id="flow1" name="theme" value="flow1" checked>
<img for="flow1" src="flow1.png" width="200px" height="200px"> 
<input type="radio" id="flow2" name="theme" value="flow2">
<img for="flow2" src="flow2.png" width="200px" height="200px">  <br>
<input style="margin-left: 325E-2%" type="radio" id="flow3" name="theme" value="flow3">
<img for="flow3" src="flow3.png" width="200px" height="200px"> 
<input type="radio" id="flow4" name="theme" value="flow4">
<img for="flow4" src="flow4.jpg" width="200px" height="200px"> 
</form>
<button onclick="createCookie()">Create cookie</button>
<button onclick="checkCookie()">Check cookie</button>

幸运的是,它创建了一个cookie,但是使用空值。我认为问题是与表单值类型,或与表单本身,但看不出我能如何解决这个问题。我是新的javascript,所以这个问题很容易为你解决。你能帮我找出我的错误吗?

代码完全没问题,问题不在于代码,而在于我的浏览器。主要的问题是Google Chrome不允许从本地文件制作cookie,并且至少需要一个服务器。如果您在Firefox中打开这段代码,它将工作,但直到您关闭浏览器

相关内容

  • 没有找到相关文章

最新更新