繁体   English   中英

使用PHP和JavaScript显示/禁用按钮

[英]Show/disable button with PHP and JavaScript

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

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

通过将按下的时间减去现在的时间来进行比较。

我希望在自上次按下按钮后小于或等于0秒的UNIX时间戳时启用该按钮。

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

提前致谢。

你可以使用javascript或jQuery通过更改它的disabled属性来禁用该按钮。 您也可以使用javascript将其更改回来。

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

在这里阅读更多内容: http//www.w3schools.com/jsref/prop_pushbutton_disabled.asp

您可以使用jquery执行此操作,具体取决于脚本的内容。 您可以为时间分配属性,然后使用jquery setInterval函数将时间减少1秒,然后检查时间是否小于0,而不是调用ajax? 例:

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);
});

暂无
暂无

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

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