简体   繁体   English

从php程序运行时,JS Style Block不执行

[英]JS Style Block does not execute when run from a php program

I am having a strange problem. 我有一个奇怪的问题。 I am using a php if to, depend on the result, send a 1 to a js function. 我使用的是php,根据结果将1发送给js函数。 If the 1 is sent, it should block a portion of html so it can be seen. 如果发送了1,则应阻止html的一部分,以便可以看到它。 The 1 is being sent correctly because it displays in the alert, but when it gets to the command 1正确发送,因为它显示在警报中,但是当到达命令时

document.getElementById("countdown").style.display="block";

The code I am using is: 我使用的代码是:

PHP: PHP:

if ($Grade == "Kindergarten") 
{
    echo "<script language=javascript>showCountDown(1)</script>";
}

The code in my js file is: 我的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");
    }
}

Here is the strange part, at least to me, the same code works when called from the html: 这是奇怪的部分,至少对我来说,从html调用时,相同的代码有效:

<select size="1" name="Grade" id="Grade" onChange="showCountDown();">

I would like to, if possible, is Grade is "Kindergarten", display the div countdown. 我想,如果可能的话,成绩是“幼儿园”,显示div倒数。 But, I do not understand why the code is not working correctly when the correct variable is being sent. 但是,我不明白为什么在发送正确的变量时代码无法正常工作。

Can someone help? 有人可以帮忙吗?

Did you try to look if there is any javascript error on the browser's console? 您是否尝试查看浏览器的控制台上是否存在任何JavaScript错误? It should give you some clues about the error.. 它应该为您提供有关错误的一些线索。

If you have any reference error to showCountDown this could be because: 如果您对showCountDown有任何参考错误,可能是因为:

1 - You forgot to include your javascript file. 1-您忘了包含您的JavaScript文件。 You can do this on the php like this: 您可以像这样在php上执行此操作:

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

2 - You are calling the function before it is being declared 2-在声明函数之前先调用它

3 - You have other error(s) on the javascript file 3-您在JavaScript档案上有其他错误

As you said you weren't having troubles calling the function on the HTMl, I suspect that is the second option. 正如您所说,在HTMl上调用该函数没有麻烦,我怀疑这是第二种选择。

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

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