0

我有一个奇怪的问题。我正在使用 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倒计时。但是,我不明白为什么在发送正确的变量时代码不能正常工作。

有人可以帮忙吗?

4

1 回答 1

0

您是否尝试查看浏览器控制台上是否有任何 javascript 错误?它应该为您提供有关错误的一些线索..

如果您对此有任何参考错误,showCountDown可能是因为:

1 - 您忘记包含您的 javascript 文件。您可以像这样在 php 上执行此操作:

echo "<script src='your-file.js'></script>";

2 - 您在声明函数之前调用它

3 - 您的 javascript 文件有其他错误

正如您所说,在 HTMl 上调用该函数没有问题,我怀疑这是第二种选择。

于 2013-04-25T22:51:34.040 回答