[英]How do I enable a Submit button and change it's value after getting a successful ajax response in Django?
I am using Django as my web framework. 我正在使用Django作为我的Web框架。 I am creating a sort of AWS lab or AWS account.
我正在创建一种AWS实验室或AWS账户。 Initially I am disabling the Submit button after the user fills in some details like lab name, cidr block, etc. for preventing multiple submissions and at the same time I am changing the Submit button value to ' CREATING '
最初,我在用户填写了一些详细信息(例如实验室名称,cidr块等)后禁用了Submit按钮 ,以防止多次提交,同时我将Submit按钮的值更改为' CREATING '
Upon getting successful ajax response, I wish to enable the button and change the button value to CREATED and maybe refresh the page after a minute. 在获得成功的ajax响应后,我希望启用该按钮并将该按钮的值更改为CREATED,并且可能在一分钟后刷新页面。 I will add an alert for that saying that "The Page will automatically refreshed after 1 minute !"
我将为此添加一个警报,提示“该页面将在1分钟后自动刷新!”
Here is my snippet. 这是我的片段。 I am trying the below code but it doesn't work..
我正在尝试以下代码,但无法正常工作。
// handle a successful response
success : function(json) {
$('#form-labname').val(''); // remove the value from the input
$('#form-cidr').val('');
$('#form-type').val('');
$('#form-budget').val('');
$('#submit').attr("disabled", false);
$('#submit').val('Created !');
console.log(json); // log the returned json to the console
console.log("success"); // another sanity check
$("#result").prepend("<br><br><h3>Lab Creation Success</h3><br><li>
<strong>Account Number: "+json.account+"</strong></li><br><li>
<strong>Lab Name: "+json.labname+"</strong></li><br><li>
<strong>Email ID: "+json.email+"</strong></li><br><li><strong>New
VPC ID: "+json.vpcid+"</strong></li><br>");
console.log("success"); // another sanity check
},
My Submit button's id is 'submit'.. I have tried to enable and change it's value in the 5th and the 6th lines of the successful response handling code. 我的“提交”按钮的ID为“提交”。我试图在成功的响应处理代码的第5行和第6行中启用和更改它的值。 What am I doing wrong and how should I refresh the page after a minute ?
我在做什么错,一分钟后应该如何刷新页面? Please help.
请帮忙。 Thanks in advance
提前致谢
Here is the html snippet : 这是html片段:
<form method="post" id="form-post">{% csrf_token %}
<div class="form-group">
<label for="form-labname">Lab Name</label><br>
{{form.labname}}
</div>
<div class="form-group">
<label for="form-cidr">CIDR Block</label><br>
{{form.cidr}}
</div>
<div class="form-group">
<label for="form-budget">Budget</label><br>
{{form.budget}}
</div>
<input id="submit" type='submit' class="btn btn-primary"
style="background-color:#3facdb;border-color=#3facdb;" value='Submit' />
</form>
And below is the JQuery I used initially to disable the Submit button and change it's value to "Creating" : 下面是我最初用来禁用Submit按钮并将其值更改为“ Creating”的JQuery:
$(document).ready(function () {
$("#form-post").submit(function () {
$("#submit").attr("disabled", true);
$("#submit").val("Creating ...");
return false;
//$("#submit").val("Creating ...");
});
});
Can Javascript be used to change the button value instead ? 可以使用Javascript来更改按钮值吗? And enable it at the same time ?
并同时启用它?
You should use prop
to set disabled state of the button 您应该使用
prop
来设置按钮的禁用状态
$('#submit').prop("disabled", false);
If your button is a <button>
button, you'll have to use .text()
to change its text. 如果您的按钮是
<button>
按钮,则必须使用.text()
更改其文本。
$('#submit').text('Created !');
ps naming a form element submit
can cause you some trouble, ie form.submit()
ps命名表单元素
submit
form.submit()
您带来一些麻烦,即form.submit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.