简体   繁体   English

单击后禁用提交按钮,并保持POST / REDIRECT / GET工作

[英]Disable submit button after click and keep POST/REDIRECT/GET working

I use the POST/REDIRECT/GET trick to prevent refresh-resend of forms. 我使用POST / REDIRECT / GET技巧来防止重新发送表单。 Now I would like to disable a form submit button (after click) that should not be clicked twice. 现在,我想禁用不应两次单击的表单提交按钮(单击后)。 Although I tried all javascript examples I found, they all conflict with POST/REDIRECT/GET. 尽管我尝试了所有发现的JavaScript示例,但它们均与POST / REDIRECT / GET冲突。 In most cases it doesn't even submit but just redirects to itself. 在大多数情况下,它甚至不提交,而只是重定向到自身。 Any solution ? 有什么办法吗? Thanks for your help. 谢谢你的帮助。

Something I've tried and conflicts is this example: 我尝试过的一些事情和冲突就是这个例子:

the script first runs headers.php with this in: 该脚本首先在其中运行headers.php:

if (isset($_POST['start-diff-scan']))
{
    $_SESSION['SCAN_START'] = true;

    header('HTTP/1.1 303 See Other');
    header('Location: '.APP_URL.'scan.php');
    exit;
}

and then scan.php with this form: 然后使用以下形式进行扫描:

<form name="start-diff-scan" id="start-diff-scan" method="post">
    <button
        name="start-scan"
        id="start-scan"
        value=""
        class="start-scan btn btn-primary"
        type="submit"
        method="post">
        Start New Scan
    </button>
</form>

and works fine. 并且工作正常。 but the js trick conflicts this. 但是js技巧与此矛盾。 I added the js code at the very end of scan.php 我在scan.php的最后添加了js代码

<script>
$('#start-diff-scan').submit(function()
{
    $('#start-scan').prop('disabled', true);
});
</script>

What happens is it only redirects to the same page (scan.php) without executing anything. 发生的事情是它仅重定向到同一页面(scan.php),而不执行任何操作。 Thanks for all yoru answers and help so far this is great. 到目前为止,感谢您提供的所有yoru解答和帮助,这很棒。 Any ideas appreciated thanks very much 任何想法表示感谢

You cannot prevent it this way, since it's user-side scripting, and people can easily delete this code, before sending the form. 您无法通过这种方式阻止它,因为它是用户端脚本,人们可以在发送表单之前轻松地删除此代码。 Even you disable the button. 即使禁用按钮。 Make checks in your server-side script, whether the request is already inserted, so you will display them either an error, or get them back to the page 在服务器端脚本中进行检查,是否已插入请求,因此您将显示错误或将其返回页面

$('#start-scan').attr("disabled", true);

应该管用

If the "disable" method is messing up your code and all else fails : 如果“禁用”方法使您的代码混乱,而其他所有方法均失败:

You can try to overlay the button with a "loader gif" making it not clickable. 您可以尝试用“ loader gif”覆盖按钮,使其不可单击。

$('#start-scan').click(function()
{
    $('#start-scan').before('<div>LOADING</div>'); //Set the loading overlay width, height and background-color
});

The problem you have is setting the disabled when the person clicks causes the button to be disabled and the click action does not fire off. 您遇到的问题是,当用户单击时将其设置为禁用会导致按钮被禁用,并且单击操作不会触发。

Add a setTimeout to delay setting the disabled property. 添加setTimeout以延迟设置禁用的属性。

Other option is to hide the button and replace it with text that says submitting. 另一种选择是隐藏按钮,并用表示提交的文本替换。

$('#start-scan').attr('disabled', true);

or use 或使用

.prop("disabled", true)

$('#start-scan').prop("disabled", true)

您可以将其添加为以下形式:

onClick="this.disabled=true"

尝试prop()

$('#start-scan').prop('disabled', true);

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

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