簡體   English   中英

JavaScript - “標識符已經聲明”

[英]JavaScript - “Identifier has already been declared”

在使用localStorage獲取注冊表/登錄表單以處理HTML時遇到問題。 作為我的任務的一部分,我不能使用數據庫或PHP,即使這些更容易。 特別是在我的提交功能期間發生了問題。

返回的語法錯誤是“已聲明標識符registerUser”,並在控制台中突出顯示const reponse = processUser (username.value, password.value)

這是我的JavaScript代碼:

//setup form
window.addEventListener("DOMContentLoaded", setupForm);

function setupForm() {
    const form = document.getElementById("login");
    form.addEventListener("submit", handleSubmit);
}

//handle Submit
function handleSubmit(event) {
    event.preventDefault();

    const {
        Username,
        Password,
        login_type,
        result
    } = event.target;

    const processUser = login_type.value == "register" ? registerUser : loginUser;
    const response = processUser(username.value, password.value);
    result.innerHTML = response;
}

//register new user
function registerUser(username, password) {
    window.localStorage.setItem("username", username);
    window.localStorage.setItem("password", password);

    return 'New user ${username} now registered!';
}

//login existing user
function loginUser(username, password) {
    const registeredUser = window.localStorage.getItem("username", username);
    const registeredUser = window.localStorage.getItem("password", password);

    const validUser = username == registeredUser;
    const validPassword = password == registeredUser;

    if (validUser && validPassword) {
        return 'User ${username} successfully logged-in!';
    }
    //error form for incorrect username or password
    else if (!validUser) return 'Incorrect username or password';
}

HTML表單:

<fieldset>
    <form id="login" action="submit" method="post">
        <legend><b>Login here!</b></legend><br />
        <div>
            <label for="username">Username:</label>
            <input id="username" type="text" name="username" required />
        </div>
        <div>
            <label for="password">Password:</label>
            <input id="password" type="password" name="Password" required />
        </div>
</fieldset>
<span>
    <input id="login" type="hidden" name="login_type" value="login" checked="checked"></input>
    <!--Hidden for the purposes of my script working-->
    <input type="submit" value="Submit" /><br /><br />
    <output name="result"></output>
    </fieldset>
</span>

任何幫助將不勝感激,因為這非常令人沮喪!

2個變量具有已registeredUser的同名用戶。

const registeredUser        = window.localStorage.getItem("username", 
username);
const registeredUser        = window.localStorage.getItem("password", 
password);

因此,我沒有嘗試使用registeredUser ,而是將其更改為registeredPassword

//login existing user
function loginUser(username, password){

const registeredUser        = window.localStorage.getItem("username", 
username);

const registeredPassword      = window.localStorage.getItem("password", 
password);

const validUser                 = username === registeredUser;
const validPassword         = password === registeredPassword;

也使用===而不是== 原因

你宣布了​​兩次標識符:

const registeredUser        = window.localStorage.getItem("username", 
username);
const registeredUser        = window.localStorage.getItem("password", 
password);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM