[英]Javascript Enable / Disable Button Not Working
在开始之前,我已经阅读了许多其他与该主题相关的文章,例如; 如何使用javascript禁用和重新启用按钮? 尽管这些帖子似乎都没有解决我的问题。
我正在使用HTML,CSS和javascript在Dreamweaver CS6中创建一个移动应用程序。 该应用程序很简单,包括一个用于控制计时器运行的“开始”和“停止”按钮(即使手机处于关机状态,该计时器也会计数,因为它会根据数据库中存储的日期时间值计算出时间差。)
我想要的是,在计时器正在运行时,禁用启动按钮和启用停止按钮,以及在计时器未运行时,将其反转。 我尝试了以下几种方法,这些方法在不同程度上起作用。
function runningTimer()
{
var timerStarted = localStorage.getItem("timerStarted");
if (timerStarted == "true")
{
document.getElementById("runningTime").style.color="#00CC00";
document.getElementById("stopButton").removeAttribute('disabled');
document.getElementById("startButton").setAttribute("disabled", "disabled");
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM temp WHERE tempID=?", [1], function(tx, result) {
for (var i = 0; i < result.rows.length; ++i) {
var row = result.rows.item(i);
var sqlStartTime = row['startTime'];
}
if (!result.rows.length)
{
alert("Error Code 420: Start Time not found.");
}
var currentTime = new Date();
var jsStartTime = convertSQLtoJS(sqlStartTime);
var shiftTimeMS = currentTime - jsStartTime;
var shiftTime = new Date(null, null, null, null, null, null, shiftTimeMS);
var shiftHours = shiftTime.getHours();
if (shiftHours < 10)
{
shiftHours = "0" + shiftHours;
}
var shiftMinutes = shiftTime.getMinutes();
if (shiftMinutes < 10)
{
shiftMinutes = "0" + shiftMinutes;
}
var shiftSeconds = shiftTime.getSeconds();
if (shiftSeconds < 10)
{
shiftSeconds = "0" + shiftSeconds;
}
document.getElementById("runningTime").innerHTML = (shiftHours + ":" + shiftMinutes + ":" + shiftSeconds);
});
});
}
else if (timerStarted == "false")
{
document.getElementById("runningTime").style.color="#FF0000";
document.getElementById("stopButton").setAttribute("disabled", "disabled");
document.getElementById("startButton").removeAttribute('disabled');
}
else
{
document.getElementById("stopButton").setAttribute("disabled", "disabled");
document.getElementById("startButton").removeAttribute('disabled');
}
}
上面的代码在您第一次使用时不起作用。 开始时正确禁用了“停止”或“开始”按钮(取决于天气或加载页面/应用程序时计时器是否已在运行。)但是一旦单击“停止”或“开始”按钮,直到刷新后才重新启用按钮在浏览器中找到该页面,或关闭并在智能手机上重新打开该应用。 因此,我在开始和停止按钮上都添加了onClick页面加载功能,该功能在Google Chrome浏览器中可以完美运行,但是在智能手机上无法正常运行。
我还尝试了以下方法,这些方法也无法正常工作;
document.getElementById("stopButton").disabled=true;
document.getElementById("startButton").disabled=false;
我有几个朋友为我查看代码,每个人似乎都认为它应该工作,所以一旦一切正常,重新加载页面后,我们所有人都将目光投向了问题所在。
任何帮助都是非常棒的,我敢肯定我刚刚做过一些愚蠢的事情,但是请告诉我!
亲切的问候,Mitchell Ransom
好的,自从我问这个问题以来已经有一段时间了,我仍然离解决这个问题还很近。 我决定完全删除该功能。 并不是真正的答案,但是由于似乎没有解决方案,因此它是唯一能够继续进行项目其余部分的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.