在表单提交上禁用提交按钮


77

单击后,我编写了以下代码以禁用网站上的提交按钮:

$('input[type=submit]').click(function(){
    $(this).attr('disabled', 'disabled');
});

不幸的是,它没有发送表格。我怎样才能解决这个问题?

编辑 我想绑定提交,而不是表格:)

Answers:


152

做到onSubmit()

$('form#id').submit(function(){
    $(this).find(':input[type=submit]').prop('disabled', true);
});

发生的事情是您实际上在完全触发该提交事件之前禁用了该按钮。

您可能还应该考虑使用ID或CLASS来命名元素,因此不要在页面上选择所有提交类型的输入。

演示: http //jsfiddle.net/userdude/2hgnZ/

(请注意,我使用preventDefault()return false因此该表单在示例中并未实际提交;请在使用时将其保留。)


是的,实际上我真的很想绑定页面中的所有提交。因此,我不介意以这种方式使用ID或CLASS :)尝试使用$('input[type=submit]').submit(function():已发送表单,但该按钮将不再禁用...
markzzz 2011年

4
您需要submit()在表单上输入内容,而不是输入内容。观看演示。
贾里德·法瑞什

是的,它有效!唯一的问题是,我在窗体上有一个onSubmit函数,因此可以通过jquery阴影绑定它!我需要在我的原始函数上实现此功能,或者更改整个调用!谢谢
markzzz 2011年

那是我发现奇怪的是,我不能在提交上绑定单击事件,而在表单提交上又绑定另一个:)
markzzz 2011年

2
就我而言,我需要将其包装在$(document).ready中以使其工作。可能很明显。显而易见的是,如果正在接收POST(或GET)的程序正在寻找Submit按钮的值,如果它被禁用,则它将不包括在POST值。至少那是我的测试所显示的。
Craig Jacobs 2014年

22

特别是如果有人遇到以下问题Chrome

要解决此问题,您需要使用元素中的onSubmit标签<form>设置提交按钮disabled。这将允许Chrome在按下按钮后立即禁用该按钮,并且表单提交仍将继续进行...

<form name ="myform" method="POST" action="dosomething.php" onSubmit="document.getElementById('submit').disabled=true;"> 
     <input type="submit" name="submit" value="Submit" id="submit"> 
</form>

这不是开发人员想要的主要部分。Javascript内容不应在HTML代码中内联。可能使用.on('submit', function() {..})(或.submit(function() {..}))会更好。即使在这种情况下,您也可能会.submit()从回调内部重新触发表单而陷入无限循环(至少在我正在使用的Opera的最新版本中,该引擎应使用与Chrome相同的引擎)。
卡马菲瑟(Kamafeather),2015年

7
这正是我想要的。一个简单的内联,不需要外部库,即使禁用了JavaScript也不会陷入困境。
SeppoErviälä15年

在具有JQuery验证的表单上对此进行了测试,该表单可以防止验证失败的情况下提交,最终导致此答案仍然禁用了提交按钮。
肖恩·德威特

13

禁用的控件不会提交其值,这不利于了解用户是否单击了保存或删除。

所以我将按钮值存储在确实提交的隐藏值中。隐藏的名称与按钮名称相同。我将所有按钮都称为button

例如 <button type="submit" name="button" value="save">Save</button>

基于此我在这里找到。只需将单击的按钮存储在变量中即可。

$(document).ready(function(){
    var submitButton$;

    $(document).on('click', ":submit", function (e)
    {
        // you may choose to remove disabled from all buttons first here.
        submitButton$ = $(this);
    });

    $(document).on('submit', "form", function(e)
    {
        var form$ = $(this);
        var hiddenButton$ = $('#button', form$);
        if (IsNull(hiddenButton$))
        {
            // add the hidden to the form as needed
            hiddenButton$ = $('<input>')
                .attr({ type: 'hidden', id: 'button', name: 'button' })
                .appendTo(form$);
        }
        hiddenButton$.attr('value', submitButton$.attr('value'));
        submitButton$.attr("disabled", "disabled");
    }
});

这是我的IsNull职能。使用或替换您自己的版本为IsNull或undefined等。

function IsNull(obj)
{
    var is;
    if (obj instanceof jQuery)
        is = obj.length <= 0;
    else
        is = obj === null || typeof obj === 'undefined' || obj == "";

    return is;
}


4

简单有效的解决方案是

<form ... onsubmit="myButton.disabled = true; return true;">
    ...
    <input type="submit" name="myButton" value="Submit">
</form>

资料来源:这里


2

更简单的方法。我已经尝试过了,对我来说效果很好:

$(':input[type=submit]').prop('disabled', true);

2
这是做什么的?这对于上述禁用按钮不会发送表格的问题有何帮助?
phil294

这将阻止提交表单。询问者希望该表单即使在禁用按钮后也能继续提交。
吴彦祖

1

您的代码实际上可以在FF上运行,而不能在Chrome上运行。

这适用于FF和Chrome。

$(document).ready(function() {
        // Solution for disabling the submit temporarily for all the submit buttons.
        // Avoids double form submit.
        // Doing it directly on the submit click made the form not to submit in Chrome.
        // This works in FF and Chrome.
        $('form').on('submit', function(e){
          //console.log('submit2', e, $(this).find('[clicked=true]'));
          var submit = $(this).find('[clicked=true]')[0];
          if (!submit.hasAttribute('disabled'))
          {
            submit.setAttribute('disabled', true);
            setTimeout(function(){
              submit.removeAttribute('disabled');
            }, 1000);
          }
          submit.removeAttribute('clicked');
          e.preventDefault();
        });
        $('[type=submit]').on('click touchstart', function(){
          this.setAttribute('clicked', true);
        });
      });
    </script>

0

如何禁用提交按钮

只需在onclick事件上调用函数,然后...返回true即可提交,返回false即可禁用提交。 或者 在window.onload上调用一个函数,例如:

window.onload = init();

并在init()中执行以下操作:

var theForm = document.getElementById(‘theForm’);
theForm.onsubmit =  // what ever you want to do 

0

以下为我工作:

var form_enabled = true;
$().ready(function(){
       // allow the user to submit the form only once each time the page loads
       $('#form_id').on('submit', function(){
               if (form_enabled) {
                       form_enabled = false;
                       return true;
               }

               return false;
        });
});

如果用户尝试多次提交表单(通过单击提交按钮,按Enter等),则这将取消提交事件。


0

我一直在使用blockUI来避免浏览器在禁用或隐藏的按钮上不兼容。

http://malsup.com/jquery/block/#element

然后我的按钮有一个类自动按钮:

  $(".autobutton").click(
     function(event) {
        var nv = $(this).html();
        var nv2 = '<span class="fa fa-circle-o-notch fa-spin" aria-hidden="true"></span> ' + nv;
        $(this).html(nv2);
        var form = $(this).parents('form:first');

        $(this).block({ message: null });
        form.submit();                
        });   

然后是这样的形式:

<form>
....
<button class="autobutton">Submit</button>   
</form>

0

按钮代码

<button id="submit" name="submit" type="submit" value="Submit">Submit</button>

禁用按钮

if(When You Disable the button this Case){
 $(':input[type="submit"]').prop('disabled', true); 
}else{
 $(':input[type="submit"]').prop('disabled', false); 
}

注意:这次您的案例可能是多个,可能需要更多条件


0

是否也要提交按钮的值并防止重复提交?

如果您使用的是类型为提交的按钮,并且还希望提交按钮的值(如果禁用了按钮则不会发生),则可以设置表单数据属性,然后进行测试。

// Add class disableonsubmit to your form
    $(document).ready(function () {
        $('form.disableonsubmit').submit(function(e) {
            if ($(this).data('submitted') === true) {
                // Form is already submitted
                console.log('Form is already submitted, waiting response.');
                // Stop form from submitting again
                e.preventDefault();
            } else {
                // Set the data-submitted attribute to true for record
                $(this).data('submitted', true);
            }
        });
    });
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.