javascript 第二次单击时 jQuery 禁用按钮功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19640663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:18:57  来源:igfitidea点击:

jQuery Disable Button function on second click

javascriptjquery

提问by Alexander C.

I'm trying to prevent users from clicking the button on the page twice, so what I've been doing is hiding that button with jQuery after it's been clicked once. But is there a way instead of hiding that button to disable with jQuery that button after it was clicked once?

我试图阻止用户单击页面上的按钮两次,所以我一直在做的是在单击一次后用 jQuery 隐藏该按钮。但是有没有办法而不是隐藏该按钮以在单击一次后使用 jQuery 禁用该按钮?

I've tried

我试过了

$(document).ready(function () {
    $('#onClickHideButton').click(function () {
        $(this).prop('disabled', true);
    });
});

but the problem that I'm having with this function is that as soon as the button is clicked it it becomes disabled before it get the chance to submit the form, so the form never gets submitted.

但是我在使用此功能时遇到的问题是,一旦单击按钮,它就会在有机会提交表单之前被禁用,因此表单永远不会被提交。

回答by NDM

Why not disable the button when the form is submitted, since that's what you actually want to do...

为什么不在提交表单时禁用该按钮,因为那是您真正想要做的...

$('#myForm').on('submit', function () {
    $('#myButton').prop('disabled', true);
});

回答by Bobby Shark

Instead of using a submit button, just use a simple button and send manually the form submit.

而不是使用提交按钮,只需使用一个简单的按钮并手动发送表单提交。

<input type="button" id="onClickHideButton" value="Submit"/>

$('#onClickHideButton').click(function () {
     $(this).prop('disabled', true);
     $('#myform').submit();
});

or you could disable the button when the form is submitted

或者您可以在提交表单时禁用该按钮

$('#myform').on('submit', function(e) { 
    $('#onClickHideButton').prop('disabled',true);
});