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);
}
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 库。所以我想,我的错误在于那条鳗鱼。我会感激你的任何帮助!
解决方案
您应该阅读此内容以了解 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.
}
现在无需更改第二个功能
推荐阅读
- plsql - 事务管理相关问题
- javascript - 将 Vtt 文件从 express 服务器发送到客户端
- flutter - 通过插入视频链接从 tiktok 下载视频。颤振/飞镖
- eclipse-cdt - eclipse上CppStyle插件的问题
- javascript - 我正在尝试安装 Material Top Tabs Navigator React Native 6.xx 版,但出现错误
- html - 当页面包含溢出表格时,HTML 电子邮件的视口缩放不起作用
- reactjs - React JS:使用 setInterval() 触发函数以每秒执行一次
- r - 获取虚拟变量的百分比
- javascript - webpack:没有 css 模块的 css 加载器 LocalIdentName
- groovy - 无法使用 groovy 从 SOAP UI 获取响应值