gpt4 book ai didi

javascript - 如何用javascript读取和删除Cookie?

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

allCookies 内容是我的浏览器 Cookie 列表。我想用 delCookie() 这个函数删除 Cookie,但它只删除第一个 Cookie,而不删除其他 Cookie。我该如何更新 cookie???

<input type="button" value="DElete" onclick="delCookie()">
<input type="button" value="Update" onclick="modCookie()">


<select multiple id="allCookies" size="5">
<!-- Cookies content-->
</select><br>

function delCookie() {
if (confirm('Are u sure?')) {
var e = document.getElementById("allCookies");
var strUser = e.options[e.selectedIndex].value;
document.cookie = encodeURIComponent(strUser) + "=deleted; expires=" + new Date(0).toUTCString();
}
}

最佳答案

It just delete the first Cookie, and not the others cookies.

selectedIndex <select> 的属性元素仅返回第一个选定选项的索引。要检查 multiple 中的所有内容选择,您将需要迭代选项集合:

var os = document.getElementById("allCookies").options;
for (var i=0; i<os.length; i++) {
if (os[i].selected) {
var strUser = os[i].value;

}
}

And how could i do to update a cookie???

只需覆盖 cookie,即使用与创建它们时相同的方法。

关于javascript - 如何用javascript读取和删除Cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14811421/

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