[英]Global variables not changing through function
我试图获取一个人当前在键盘输入中使用的输入框的值和名称,但是一旦键盘输入功能运行后,全局变量就无法更新。 他们只是继续说在Google Chrome开发人员工具中未定义。
我究竟做错了什么?
$( document ).ready(function() {
var assessName;
var assessVal;
var impName;
var impVal;
//Get the name and value of the currently selected assessed input
$('[id^=ass]').on('keyup', function(){
assessName = $(this).attr('name').replace('ass-','');
assessVal = $(this).val();
});
//Get the name and value of the currently selected implemented input
$('[id^=imp]').on('keyup', function(){
impName = $(this).attr('name').replace('imp-','');
impVal = $(this).val();
});
console.log(assessName);
console.log(assessVal);
console.log(impName);
console.log(impVal);
});
发生这种情况是因为keyup
事件在将variables
写入控制台后会更改variables
。 JavaScript中的事件是异步的 。
因此,如果将console.log
调用放入事件侦听器中,它将完全显示您的期望。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.