表单提交后未定义(javascript)

表单提交后未定义(javascript),javascript,html,forms,Javascript,Html,Forms,请帮我做这个。我想在用户成功登录后将用户名存储在cookie中。但是在单击登录按钮后,表单是未定义的,我无法从中使用addEventListener属性。因此,不会创建cookie。如果loginForm的条件未定义(在createEventListener()函数原型中),Chrome浏览器会在中给我一个错误 index.htm <form action="results.htm"> <fieldset id="deliveryinfo"> &l

请帮我做这个。我想在用户成功登录后将用户名存储在cookie中。但是在单击登录按钮后,表单是未定义的,我无法从中使用
addEventListener
属性。因此,不会创建cookie。如果
loginForm
的条件未定义(在
createEventListener()
函数原型中),Chrome浏览器会在
中给我一个错误

index.htm

<form action="results.htm">
    <fieldset id="deliveryinfo">
        <legend>Site Login</legend>
        <label for="usernameinput">Username</label>
        <input type="text" id="usernameinput" name="username" />
        <label for="passwordinput">Password</label>
        <input type="password" id="passwordinput" name="password" />
    </fieldset>
    <fieldset id="submitbutton">
        <input type="submit" id="submitBtn" value="Login" />
    </fieldset>
</form>

您需要设置过期日期。若你们并没有设置expires,那个么cookie是会话绑定的&当你们离开页面(提交)时,它就会被销毁。添加“path=/”还可以在所有站点页面上使用cookie

function processCookie() {
    //cookie lifetime in milliseconds (24 hours)
    var date = new Date(new Date().getTime() + 24 * 3600 * 1000); 
    document.cookie = "username=" +document.getElementById('usernameinput').value+"; path=/; expires=" + date.toUTCString();
}

客户端身份验证没有任何意义。提交后页面停止存在,没有js会运行。没错,在现实生活中的网站中,输入的用户名和密码会提交到web服务器进行验证(我还没有:)。我刚刚习惯了在需要登录以保存用户登录名的站点上使用cookie。我将在下一步中设置过期日期,但现在我需要在用户重新加载或刷新页面后使用cookie中存储的用户名填充用户名字段。
function processCookie() {
    //cookie lifetime in milliseconds (24 hours)
    var date = new Date(new Date().getTime() + 24 * 3600 * 1000); 
    document.cookie = "username=" +document.getElementById('usernameinput').value+"; path=/; expires=" + date.toUTCString();
}