显示/禁用按钮与PHP和JavaScript

Show/disable button with PHP and JavaScript

本文关键字:PHP JavaScript 按钮 显示      更新时间:2023-09-26

当UNIX时间戳达到<= 0时,我该如何禁用/启用按钮呢?

我使用AJAX每隔250毫秒连续调用一个.php文件。在这个.php文件中,我通过比较(在UNIX中)现在和上次按下按钮(使用MySQL数据库)的时间来检查自上次操作以来是否已经过了90秒。

比较是通过将按下的时间减去现在的时间来完成的。

我想启用按钮时,当UNIX时间戳,当有小于或等于0秒的按钮上次按下

我知道PHP和JavaScript之间的合作是一个挑战,但我也知道有很多方法可以做到这一点。

您可以根据脚本的内容使用jquery完成此操作。而不是调用ajax,你可以分配一个属性的时间,然后使用jquery的setInterval函数减少1秒的时间,然后检查时间是否小于0或不?例子:

HTML:

<input type='submit' id='button' name='button' data-time='(using php, put the seconds in here' />
Jquery

$(document).ready(function() {
var timer = setInterval(function() {
var current = $("#button").data('time');
// get the current value of the time in seconds
var newtime = current - 1;
if (newtime <= 0) {
// time is less than or equal to 0
// so disable the button and stop the interval function
$("#button").prop('disabled', true);
clearInterval(timer);
} else {
// timer is still above 0 seconds so decrease it
$("#button").data('time', newtime);
}
}, 1000);
});

你可以使用javascript或jQuery通过改变它的disabled属性来禁用按钮。你也可以用javascript把它改回来。

示例:document.getElementById("myBtn").disabled = true;

阅读更多:http://www.w3schools.com/jsref/prop_pushbutton_disabled.asp