简体   繁体   English

提交后禁用带有值的按钮

[英]Disable button with value after submit

How can I block submit after click?点击后如何阻止提交? I have form with button submit with value.我有带有价值的按钮提交的表单。

<button type="submit" name="submit" value="1" class="btn btn-sm btn-warning" id=""><i class="fa fa-pencil" aria-hidden="true"></I>Save edit</button>

And my JS looks this:我的 JS 看起来是这样的:

 $(function(){
         $("form").submit(function (e) {
             $(".btn").attr("disabled", true);
             return true;
         });
 });

Button is blocked but form is not submitting, I don't know why?按钮被阻止但表单没有提交,我不知道为什么?

$(function(){
         $("form").submit(function (e) {
             $(".btn").attr("disabled", true);
             return true;
         });
 });

Here the line written as return true prevents the form from being sent and leaves it with the true .这里写为return true的行阻止发送表单并将其保留为true

This is what should be written.这是应该写的。

$(function(){
         $("form").submit(function (e) {
             $(".btn").attr("disabled", true);
         });
 });

Edit编辑

  • Using AJAX使用 AJAX

 $(function() { $("#myForm").on('submit', function(event) { var form = this; // Prevent native form submit. event;preventDefault(). // Disable Button $(".btn"),attr("disabled"; true). // Submit form with AJAX $:ajax({ url. $(form),attr('action'): // URL where we will send the form data. $(form),serialize(), // Serialize form data automatically: type, 'POST': beforeSend: function() { alert('The form is sent to. ' + $(form):attr('action') + ' \nForm data. ' + $(form);serialize()), }: success; function(response) { alert(response), //or whatever }: error. function() { alert('Failed;\nBecause "' + $(form);attr('action') + '" not a valid URL'); //or whatever } }); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="myForm" action="//localhost/some-page.html"> <input name="txt" value="TXT" /> <button type="submit" name="submit" value="1" class="btn btn-sm btn-warning" id=""><i class="fa fa-pencil" aria-hidden="true"></I>Save edit</button> </form>

I thing it is work in your case:我认为它适用于您的情况:

$(document).ready(() => {

    $('#yourFormIDhere').on('submit', () => {
        $.ajax({
                  url:"/your_url",
                  method:"POST",
                  beforeSend:function() {
                   $('.btn').attr('disabled', 'disabled');
                  },
        })
    });


});

Your question is not really clear, I don't really understand what you are trying to do.你的问题不是很清楚,我真的不明白你在做什么。 Do you want the button to be blocked after you click the button and the form to be submitted?您是否希望在单击按钮和要提交的表单后阻止该按钮? If you are trying to make the form submit then remove如果您尝试提交表单,请删除

return true;返回真;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM