【发布时间】:2017-10-22 22:17:09
【问题描述】:
使用 AJAX,我正在检查数据库中是否存在电子邮件或用户名。如果两者之一(或两者)存在,我想禁用提交按钮。
现在我设法为输入边框着色,但我被困在禁用按钮的逻辑上。
我使用以下方法禁用提交按钮:
document.getElementById("submit").disabled = true;
我应该在哪里应用什么逻辑来禁用按钮?
$(function() {
$("#username").on("change", function() {
var user = document.forms["reg_form"]["username"].value;
$.ajax({
type: 'POST',
url: 'api/checkdata.php',
data: {user: user},
dataType: 'json',
success: function(r) {
if(r=="1") {
//Exists
$("#username").css("border-color", "#ff0000");
} else {
//Doesn't exist
$("#username").css("border-color", "#2cbc21");
}
}
});
});
$("#email").on("change", function() {
var email = document.forms["reg_form"]["email"].value;
$.ajax({
type: 'POST',
url: 'api/checkdata.php',
data: {email: email},
dataType: 'json',
success: function(r) {
if(r=="1") {
//Exists
$("#email").css("border-color", "#ff0000");
} else {
//Doesn't exist
$("#email").css("border-color", "#2cbc21");
}
}
});
});
});
【问题讨论】:
标签: javascript jquery html ajax