function toMem(a) {
newCookie('theUsername', document.forms["login-form"]["username"].value);
newCookie('thePassword', document.forms["login-form"]["password"].value);
}
function delMem(a) {
eraseCookie('theUsername');
eraseCookie('thePassword');
//document.forms["login-form"]["username"].value = ''; i commented this out because
//document.forms["login-form"]["password"].value = ''; you dont want to delete the values if a user unchecks remember me
}
javascript timeout function in onSubmit login-form for RememberMe
-
22-09-2022 - |
Вопрос
I'm using javascript for storing cookies in my login form(Remember Me checkbox) and I would want that if user checks the box = cookies saved, and if he unchecks it = cookies deleted(that's ofc if they are saved). That should all happen when the user submits the form(login). It works when for example I put some button so when I click it, cookies are deleted. This is my form:
<form name="login-form" class="login-form" action="login_exec.php" onSubmit="if(this.checker.checked) toMem(this)" method="post">
These are my js functions:
function toMem(a) {
newCookie('theUsername', document.forms["login-form"]["username"].value);
newCookie('thePassword', document.forms["login-form"]["password"].value);
}
function delMem(a) {
eraseCookie('theUsername');
eraseCookie('thePassword');
document.forms["login-form"]["username"].value = '';
document.forms["login-form"]["password"].value = '';
}
Ok so, I tried to make it like this:
<form name="login-form" class="login-form" action="login_exec.php" onSubmit="if(this.checker.checked) {toMem(this)} else setTimeout(delMem(this), 3000)" method="post">
It didn't work... It deleted the input before the form was submitted. So anyone got an idea?
Решение
Другие советы
"this", inside of setTimeout refers to the window object. Also, setTimeout expects to be passed a function, not an invocation.
better to use a listener here, but I think this should work...
onSubmit="var that = this;if(this.checker.checked) {toMem(this)} else setTimeout(function(){delMem(that)}, 3000)"