前一個登入的表單, 有一個神奇的小勾勾, 叫 remember me,
要實作這個功能, 要先能讀到 checkbox 是否有勾, jquery 語法:
if($("#clickAll").prop("checked")) {...}
要做到這個功能, 還需要對 cookie 做存取, 提供二個簡單的 function:
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let 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 setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
let expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
與前一篇文章加起來, 完整的範例就是:
let do_signin_flag = true;
$("#fail-message").html("");
if($("#account").val().length==0){
$("#fail-message").html("Account is required.");
$("#account").focus();
do_signin_flag=false;
} else {
if($("#remember-me").prop("checked")) {
setCookie("account",$("#account").val());
}
}
相關文章
jquery check textbox empty
https://stackoverflow.max-everyday.com/2023/02/jquery-check-textbox-empty/