![](/img/trans.png)
[英]How to enable an element again after a process has been completed in Tkinter
[英]How to disable a button after clicking it and enable it after the login process is completed by checking the server response in django
我正在使用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
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.