使用表单值创建 cookie 会生成一个空 cookie
Creating cookie with a form values makes a null 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 中打开此代码,它会起作用,但直到您关闭浏览器
首先,我需要创建一个带有表单值的 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 中打开此代码,它会起作用,但直到您关闭浏览器