首页 > 解决方案 > 为什么我的函数不改变变量的值?

问题描述

当我尝试更改变量的值时遇到问题。

var id = null;
async function getUser() {
    let user = document.getElementById('user').value;
    let passs = document.getElementById('pass').value;

    let res = await eel.login(user, passs)();

    let text = res[0];

    let id_res = res[1];

    if (text == 'Вы успешно авторизовались!') {
        window.location.href = 'index.html';
    } else if (text == 'Неправильное имя пользователя или пароль') {
        alert(text);
    }

    var copy = $.extend( {}, id );
    copy = id_res; // that's my problematic part

    alert(id); // allerting updated variable
}

然后,当我尝试使用"id"变量时,它仍然返回"null"

async function createTask() {
    let task = document.getElementById('need_task').value;

    let result = await eel.create_task(copy, task);
    alert(copy); // got error, that copy is undefined
}

另外,在我的html中,我添加了document.getElementById('btn').addEventListener('click', getUser); “document.getElementById('but').addEventListener('click', createTask);” 激活我的功能。

如您所见,我使用 python eel 库。所以我想,我的错误在于那条鳗鱼。我会感激你的任何帮助!

标签: javascripthtmlfunctionreturnvar

解决方案


您应该阅读此内容以了解 javascript 中的复制:将变量的值复制到另一个

var id = null;
async function getUser() {
    let user = document.getElementById('user').value;
    let passs = document.getElementById('pass').value;

    let res = await eel.login(user, passs)();

    let text = res[0];

    let id_res = res[1];

    if (text == 'Вы успешно авторизовались!') {
        window.location.href = 'index.html';
    } else if (text == 'Неправильное имя пользователя или пароль') {
        alert(text);
    }

    id = id_res; // remove var

    alert(id); // will alert updated value.
}

现在无需更改第二个功能


推荐阅读