简体   繁体   English

使用 jQuery 提交后禁用按钮

[英]Disable button after submit with jQuery

I know there are a lot of questions about it, but I tried several solutions, and nothing works.我知道有很多关于它的问题,但我尝试了几种解决方案,但没有任何效果。

In my django app I have a form:在我的 Django 应用程序中,我有一个表单:

<form method='post'>
    <button type='submit'>Send</button>
</form>

I wan't to disable the button once the user has submitted the form.我不想在用户提交表单后禁用该按钮。 Using other questions, I tried several things, like:使用其他问题,我尝试了几件事,例如:

<button type='submit' onclick="this.disabled=true">Send</button>

When I click, the button is disabled... but the form is not submitted.当我单击时,该按钮被禁用...但未提交表单。 And for each try I had the same issue: either the button is disabled or the form is submitted.每次尝试我都会遇到同样的问题:按钮被禁用或表单已提交。 I can't find how to do both...我找不到如何做到这两点...

I'm using Chrome.我正在使用 Chrome。 Any idea on why I have this problem?知道为什么我有这个问题吗? Thank you for your help.谢谢您的帮助。

Try this:试试这个:

$('form').submit(function() {
  $(this).find("button[type='submit']").prop('disabled',true);
});

I like this, don't have to traverse the DOM.我喜欢这样,不必遍历 DOM。 Put function on a setTimeout function, this allows make submit and after disable button, even if setTimeout is 0将函数放在 setTimeout 函数上,即使 setTimeout 为 0,这也允许提交和禁用按钮后

$(document).ready(function () {
$("#btnSubmit").click(function () {
    setTimeout(function () { disableButton(); }, 0);
});

function disableButton() {
    $("#btnSubmit").prop('disabled', true);
}
});

You could disable it upon the parent form's submit event:您可以在父表单的submit事件中禁用它:

$("form").on("submit", function () {
    $(this).find(":submit").prop("disabled", true);
});

Be sure to run this code only after the HTMLFormElement has been loaded, or else nothing will be bound to it.请务必仅在加载HTMLFormElement后运行此代码,否则将不会绑定任何内容。 To ensure that the binding takes place, fire this off from within a document-ready block:为确保绑定发生,请从document-ready块中将其关闭:

// When the document is ready, call setup
$(document).ready(setup);

function setup () {
    $("form").on("submit", function () {
        $(this).find(":submit").prop("disabled", true);
    });
}

Try, like this,像这样试试

  <input type="submit" value="Send" onclick="javascript=this.disabled = true; form.submit();">

Something like this might work.这样的事情可能会奏效。

<button id="btnSubmit" type='submit'> Send </button>

<script>
     $("#btnSubmit").on("click", function(e){
          e.PreventDefault();
          $(this).closest("form")[0].submit();
          $(this).prop('disabled',true)
     });
</script>

This ended up being the best solution for me这最终成为我最好的解决方案

$("form").submit(function disableSubmit() {
  $("input[type=submit]", this).prop("disabled", true);
});

my variant, disable button, no direct disabled but only vidible hidden:我的变体,禁用按钮,没有直接禁用但仅可见隐藏:

<input type="submit" name="namebutton" value="Nahrát obrázek"  onclick="this.style.visibility='hidden';" ondblclick="this.style.visibility='hidden';"/>

You can do something like this.你可以这样做。 It is work fine with me.它对我来说很好用。

<form method='post' onSubmit='disableFunction()'>
// your code here
</form>

Then in script, add this然后在脚本中添加这个

<script>
function disableFunction() {
    $('#btn_submit').prop('disabled', true);
}
</script>

How about this?这个怎么样?

onclick="this.style.visibility='hidden';"

I would say, instead of disabled, hide it.我会说,而不是禁用,隐藏它。

If you want to go with disabled如果你想和disabled一起去

onclick="this.style.disabled='true';"

Got an issue on Chrome, wasn't submitting the form.在 Chrome 上遇到问题,无法提交表单。 Tried a bunch of different code, this was what worked best for me (and looks best imo):尝试了一堆不同的代码,这是最适合我的代码(在我看来也是最好的):

  $('#form_id').submit(function() {
    $("input[type='submit']", this)
      .val("Please Wait...")
      .attr('disabled', 'disabled');
    return true;
  });

Replace form_id with the id of your form.form_id替换为您的表单 ID。 Classes work too of course: $('.form_class')类当然也有效: $('.form_class')

Source: JavaScript Coder资料来源: JavaScript编码器

I like this better:我更喜欢这个:

<script>
    var submit = false;
    $('form').submit(function () {
        if (submit) { return false; }
        else { submit = true;}
    });
</script>

this way it also prevents the enter key to submit more than once这样它也可以防止回车键多次提交

I'm using Chrome.我正在使用 Chrome。 Any idea on why I have this problem?知道为什么我有这个问题吗?

Well, first time I dealt with this, I solved it like this:好吧,我第一次处理这个问题时,我是这样解决的:

function blockButtons() {
   $('button:submit').click(function(){
    $('button:submit').attr("disabled", true);
   });
}

This worked perfectly, but... in Mozilla Firefox.这非常有效,但是......在 Mozilla Firefox 中。 The Google Chrome did not submit it, so I changed it to this:谷歌浏览器没有提交,所以我改成这样:

function blockButtons() {
   $('button:submit').click(function(){
      var form = $(this).parents('form:first');
    $('button:submit').attr("disabled", true);
    $('button:submit').css('opacity', 0.5);
    form.submit();
   });
}

This worked both in Mozilla Firefox, however, after that some of our users using old versions of IE experienced trouble of having two submits.然而,这在 Mozilla Firefox 中都有效,之后我们的一些使用旧版本 IE 的用户遇到了两次提交的麻烦。 That is, the one initiated by the javascript, and the one by browser ignoring the fact of onclick and just submitting anyway.也就是说,一个由 javascript 启动,一个由浏览器启动,忽略 onclick 的事实,只是提交。 This can be fixed by e.preventDefault, I guess.我想这可以通过 e.preventDefault 来解决。

If you don't want an element to be double-clicked, use.one()如果您不想双击某个元素,请使用.one()

<button id="submit" type="submit">Send</button>
<script>
$(function () {
$("#submit").one("click", function () {
//enter your submit code
 });
});

.one() 。一()

You can do something like this.你可以这样做。 It is work fine with me.它对我来说很好用。

$("button#submitted").click(function () {
        $("button#submitted").prop('disabled', true);
});

Double click on your button.双击你的按钮。 This code will running这段代码将运行

You must prevent the form from being submitted more than once, disabling the button is not the right solution because the form could be submitted in other ways.您必须防止表单被多次提交,禁用按钮不是正确的解决方案,因为可以通过其他方式提交表单。

JavaScript:脚本:

$('form').submit(function(e) {
    // if the form is disabled don't allow submit
    if ($(this).hasClass('disabled')) {
        e.preventDefault();
        return;
    }
    $(this).addClass('disabled');
});

Once the form is correctly disabled, you can customize its appearance.正确禁用表单后,您可以自定义其外观。

CSS: CSS:

form.disabled {
    pointer-events: none;
    opacity: 0.7;
}

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

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