我正在 Django
和 python
中开发注册功能。当我单击“注册”按钮时,该按钮应禁用,注册成功后,它应自动再次启用。如何实现这一目标?
我尝试使用ajax
,但我不知道如何检查服务器的响应,用户是否注册成功。
最佳答案
如果您使用带有提交按钮的表单,那么一旦您点击该按钮,注册过程就会开始,并在成功注册后加载您指定的页面
如果您使用 javascript/ajax 或类似的东西将信息发送到后端然后返回,那么您可以使用以下命令禁用按钮(其中 btnSubmit 是 id 并且您正在使用 JQuery):
$("#btnSubmit").attr("disabled", true);
当你从 Django 使用中获取返回 json 时,
$("#btnSubmit").attr("disabled", false);
您的整体脚本将类似于:
function myFunction() {
$("#btnSubmit").attr("disabled", true);
var info = [];
info[0] = $('#username').val();
info[1] = $('#password').val();
$.ajax({
url : "/some_url/", // the endpoint
type : "POST", // http method
data : { info : info }, // data sent with the post request
// handle a successful response
success : function(json) {
// do stuff
$("#btnSubmit").attr("disabled", false);
},
// handle a non-successful response
error : function(xhr,errmsg,err) {
$('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
" <a href='#' class='close'>×</a></div>"); // add the error to the dom
console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
}
});
}
关于python - 如何在单击按钮后禁用按钮并在登录过程完成后通过检查 django 中的服务器响应来启用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57833670/