1

allCookies には、ブラウザの Cookie のリストが含まれています。この関数 delCookie() で 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();
    }
}
4

1 に答える 1

1

最初の Cookie を削除するだけで、他の Cookie は削除しません。

要素の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;
        …
    }
}

そして、どうすればクッキーを更新できますか???

Cookie を上書きするだけです。つまり、Cookie を作成するときと同じ方法を使用します。

于 2013-02-11T12:54:30.123 に答える