[英]How to set cookie to value of checkbox
I am trying to make a function that sets a cookie to a true/false value depending on if a checkbox has been clicked or not.我正在尝试创建一个函数,将 cookie 设置为真/假值,具体取决于是否单击了复选框。 I can then use this value when the page loads to render the checkbox as toggled.
然后,我可以在页面加载时使用此值将复选框呈现为切换状态。
HTML: HTML:
<form name="example">
<div class="checklist-item">
<label for="example">Example</label>
<input type="checkbox" id="example" name="example">
</div>
</form>
JS: JS:
document.getElementById("example").addEventListener("click", exampleCookie)
function exampleCookie() {
document.cookie = "exampleCookie";
}
How might I use the function exampleCookie
to be equal to the value of the checkbox?我如何使用函数
exampleCookie
来等于复选框的值?
sorry, i give you a referrence from here https://www.javatpoint.com/javascript-cookies maybe it helps对不起,我从这里给你一个参考https://www.javatpoint.com/javascript-cookies也许它有帮助
<!DOCTYPE html> <html> <head> </head> <body> <select id="color" onchange="display()"> <option value="Select Color">Select Color</option> <option value="yellow">Yellow</option> <option value="green">Green</option> <option value="red">Red</option> </select> <script type="text/javascript"> function display() { var value = document.getElementById("color").value; if (value != "Select Color") { document.bgColor = value; document.cookie = "color=" + value; } } window.onload = function () { if (document.cookie.length != 0) { var array = document.cookie.split("="); document.getElementById("color").value = array[1]; document.bgColor = array[1]; } } </script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.