如何在PHP中提交后禁用提交按钮? [英] How to disable a submit button after submission in PHP?

查看:84
本文介绍了如何在PHP中提交后禁用提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了关于这个问题的其他问题,但找不到满意的答案。
我有一个测验网站,用户从四个选项中选择一个选项,然后点击提交。我想在点击一次后禁用提交按钮。

I browsed through the other questions on this issue but couldn't find a satisfactory answer. I have a quiz website where the user selects an option from four options and then clicks submit. I want to disable the submit button after it has been clicked once.

有没有办法用PHP做到这一点?如果没有,有没有办法用最小的JS做到这一点。

Is there any way to do this with PHP? If not, is there a way to do this with minimal JS.

推荐答案

可用解决方案



由于您标记了jQuery这个问题,以下是一些您可以使用的简单jQuery解决方案:

要在单击它时禁用它,您可以简单地:

Available Solutions

Since you tagged the question jQuery, here are some simple jQuery solutions you can use:
To disable it when it is clicked, you can simply:

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

你可以做得更好,并且只在表单提交后禁用它:

You can do even better though, and disable it only once the form is submitted:

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



解决方案演示



以下是jsFiddle中上述逻辑的简单演示: http://jsfiddle.net/zb8CZ/

(请注意,在该实现中我没有使用jQuery,而是使用简单的JS;因为可移植性浏览器然而,以及访问jQuery提供的许多其他方法(加上语法糖!)我推荐jQuery方法。

Solution Demo

Here's a simple demo of the above logic, in a jsFiddle: http://jsfiddle.net/zb8CZ/
(Note that in that implementation I didn't use jQuery, but rather, plain JS; for portability across browsers however, as well as access to the many other methods jQuery provides (plus the syntactic sugar!) I recommend the jQuery methods.

请注意,尝试在HTML中内联实现JS事件处理(使用 onsubmit =... onclick =... attributes)被认为是不好的做法,因为它会影响你的布局/显示层的功能。您还会丢失编辑器可能提供的任何语法突出显示和/或错误检查,并且通常会使开发和维护应用程序变得更加困难,因为您的代码没有逻辑顺序。

Note that attempting to implement JS event handling inline in your HTML (using the onsubmit="..." or onclick="..." attributes) is considered bad practice, since it muddles your functionality with your layout/display layer. You also lose any syntax highlighting and/or error-checking your editor might provide, as well as just generally making it harder to develop and maintain your application, since there is no logical order to your code.

这篇关于如何在PHP中提交后禁用提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆