简体   繁体   English

在Django中获得成功的ajax响应后,如何启用“提交”按钮并更改其值?

[英]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.

相关问题 如何在AJAX请求进行时禁用提交按钮,并在收到成功AJAX响应后启用它? - How to disable a submit button when AJAX request is in progress and enable it after receiving success AJAX response? 输入的总值达到50后,如何启用提交按钮? - How do I enable my submit button after the totaled value of my inputs reaches 50? 如何在成功提交Ajax表单后获取注释类,然后显示它并进行页面外刷新? - How do I grab a comments class after a successful ajax form submit then display it with out page refresh? 如何在ajax成功响应后更改html类? - How to change html class after ajax successful response? 如何获得成功的Ajax响应后动态创建表? - How do I create a table dynamically upon getting a successful ajax response? 如何使用 AJAX POST 提交按钮值? - How do I POST submit button value using AJAX? 从ajax调用获取值后,如何在if条件中打印成功语句? - How do I print the successful statement in the if condition after getting values from an ajax call? 如何使用jquery启用表单的提交按钮? - How do I enable my form's submit button using jquery? 如何禁用div(popup)内部通过Ajax响应接收的提交按钮? - How do I disable a submit button which is received via an Ajax response inside a div(popup)? 成功的ajax请求后,Blueimp Fileupload触发提交按钮 - Blueimp Fileupload trigger submit button after a successful ajax request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM