gpt4 book ai didi

javascript如何使用表单文本输入更新cookie

转载 作者:行者123 更新时间:2023-11-28 04:38:04 26 4
gpt4 key购买 nike

我有一个 javascript 程序,它使用 cookie 来保存用户名和最喜欢的项目,在我的示例中我使用 pet。 Html 页面将允许用户通过表单文本输入更新他们最喜欢的宠物,并在单击按钮时使用输入更新 cookie。不知道如何做到这一点...任何建议或帮助将不胜感激。

<html>

<body onload="checkCookie()">
<header>
Cookies
</header>
<br>

<form>
<br>
Update favorite Pet:<br><br> <input type="text" name="fpet" id="fp"><br>
<br>
<button onclick="myUpdate()">Update</button>

</form>
<p>Favorite Pet:</p>
<p id="uip"></p>
<script>

function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
}

function checkCookie() {
var user=getCookie("username");
var pet=getCookie("fpet");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:","");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
if (pet != "") {
alert("Your favorite pet is " + pet);
} else {
pet = prompt("Please enter your favorite pet:","");
if (pet != "" && pet != null) {
setCookie("fpet", pet, 30);
}
}
document.getElementById("uip").innerHTML = pet;
}

</script>

</body>

</html>

最佳答案

你可以试试这个。当您从本地电脑运行它时,有时它无法工作。尝试将其托管在任何服务器或本地服务器上。

<html>

<body onload="checkCookie()">

<header>Cookies</header>

<br><br>
Update favorite Pet:<br><br> <input type="text" name="fpet" id="fp"><br>
<br>
<button onclick="myUpdate()">Update</button>
<p>Favorite Pet:</p>
<p id="uip"></p>

<script type="text/javascript">

function myUpdate(){
var updateFp = document.getElementById("fp").value;
setCookie('fpet',updateFp,1);
document.getElementById("uip").innerHTML = getCookie('fpet');
}

function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}

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 "";
}

function checkCookie() {
var user = getCookie("username");
var pet = getCookie("fpet");

if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:","");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
if (pet != "") {
alert("Your favorite pet is " + pet);
} else {
pet = prompt("Please enter your favorite pet:","");
if (pet != "" && pet != null) {
setCookie("fpet", pet, 30);
}
}
document.getElementById("uip").innerHTML = pet;
}

</script>

</body>

关于javascript如何使用表单文本输入更新cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44015805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com