javascript - Jquery/Javascript global variable not updated from within (nested) function
问题描述
I am trying to get a global variable updated from within a (nested) function to validate if an email address exists in the database or not. But for some reason it does not work and the variable is not updated. Does anyone has an idea what I am doing wrong?
var validate = true;
/* More stuff that works fine */
element.find("input[type=email][data-checkexist='true']").each(function() {
var email_to_check = $(this).val();
var self = $(this);
$.get('./inc/register_functions.php?checkemail=' + email_to_check, function(data) {
if(data === 'emailexist'){
self.after('<div class="alert alert-danger validate-alert">' + messages.emailexist + '</div>');
validate = false;
alert(validate); /* Returns false. Correct */
}
});
});
alert(validate); /* Returns true which is wrong */
The second alert does not return the updated value of validate. The PHP script returns the correct "emailexist" value
解决方案
推荐阅读
- java - (Person)o.getName() 和 ((Person)o).getName() 之间的区别
- java - 必需: GeneratedMessageV3.Builder<*>:如何使用值初始化 com.google.type.Money
- entity-framework-core - 从 Entity 实例创建代理
- assembly - 在 ARMv7 上使用 NEON 进行 SIMD 无符号 64 位比较 (CMHS) 的最有效方法是什么?
- flutter - 如何通过颤振更改系统DNS?
- c++ - 简化外部函数调用
- ruby-on-rails - 如何在rails中存储特定用户选择的类别和子类别
- google-bigquery - BigQuery 排序规则
- git - Git revert 无法恢复已删除的文件
- php - 从复杂的 php 数组中打印表格或列表