我遇到一个奇怪的问题。我正在使用一个php如果,根据结果,发送1到js函数。如果发送了1,它应该阻止html的一部分,以便可以看到它。1被正确发送,因为它显示在警报中,但是当它到达命令
时document.getElementById("countdown").style.display="block";
我使用的代码是:
PHP:if ($Grade == "Kindergarten")
{
echo "<script language=javascript>showCountDown(1)</script>";
}
我的js文件中的代码是:
function showCountDown(index)
{
alert("Checking for Kindergarten");
if (!index)
var n = document.getElementById("Grade").selectedIndex;
else
var n = index;
alert("Showing Index Selected: " + n);
if (n == 1)
{
alert("about to show Countdown to Kindergarten");
document.getElementById("countdown").style.display="block";
alert("Showing Countdown to Kindergarten");
}
else
{
alert("About to remove Countdown to Kindergarten");
document.getElementById("countdown").style.display="none";
alert("Removing Countdown to Kindergarten");
}
}
这里是奇怪的部分,至少对我来说,从html:
调用相同的代码工作:<select size="1" name="Grade" id="Grade" onChange="showCountDown();">
我想,如果可能的话,是年级是"幼儿园",显示div倒计时。但是,我不明白为什么代码在发送正确的变量时不能正常工作。
有人能帮忙吗?
您是否尝试查看浏览器的控制台上是否有任何javascript错误?它应该会给你一些关于错误的提示。
如果您对showCountDown
有任何引用错误,这可能是因为:
1 -你忘了包含你的javascript文件。你可以在php中这样做:
echo "<script src='your-file.js'></script>";
2 -你在函数被声明之前调用它
3 -你有其他错误的javascript文件
正如您所说,在HTMl上调用函数没有问题,我怀疑这是第二个选择。