Javascript错误,明确定义的函数未定义
因此,我有一个页面,其中 PHP 脚本回显了我的所有 HTML,包括一些 javascript。有问题的 javascript 函数应该在选择框更改时执行,但每次更改选择框时,我的 javascript 控制台都会出现错误,指出该函数未定义。
我什至将功能简化为仅显示警报框以帮助调试,但我仍然收到错误。
这是负责创建选择框的 PHP 函数:
function CreateMainSelect()
{
$html = "<select name=\"main_select\" onChange=\"test()\" id=\"main_select\" >";
$html .= "<option value='all'>All</option>";
$html .= "<option value='past'>Past</option>";
$html .= "<option value='current_and_future'>Current and Future</option>";
$html .= "<option value='date_range'>Date Range</option>";
$html .= "</select>";
return $html;
}
完全没有必要将它放在函数中,但我需要稍微修改已经存在的另一个类似函数,所以请不要对我太失望!
这是回显的 javascript 函数:
echo '<script type="text/javascript">';
echo 'function test()';
echo '{';
echo 'alert("hello world");';
echo '}';
echo '</script>';
javascript 在 PHP 文件的底部回显。我也尝试过将其移至顶部,但这并没有改变任何东西。
编辑:感谢大家的回复,您的所有建议都非常有帮助。我找出了问题所在,发现多达两处出了问题。第一个问题是我的浏览器(我认为无论如何)缓存了我的 javascript,因此我的函数的较旧的有缺陷版本被调用,这就是为什么一个完美的函数似乎会产生错误。通过清除我的缓存解决了。
另一个问题(我认为)是 JavaScript 函数实际上在执行之前并未被加载。我通过将 javascript 放在单独的 .JS 文件中并将其链接到 .PHP 文件来解决这个问题。
So, I have page where a PHP script echoes out all of my HTML including some javascript. The javascript function in question is supposed to be executed when a select box is changed but everytime I change the select box I get an error in my javascript console saying that the function is undefined.
I've even reduced the function to simply displaying an alert box to help in debugging but I'm still getting the error.
Here's the PHP function responsible for creating the select box:
function CreateMainSelect()
{
$html = "<select name=\"main_select\" onChange=\"test()\" id=\"main_select\" >";
$html .= "<option value='all'>All</option>";
$html .= "<option value='past'>Past</option>";
$html .= "<option value='current_and_future'>Current and Future</option>";
$html .= "<option value='date_range'>Date Range</option>";
$html .= "</select>";
return $html;
}
Totally unnecessary to have it in a function but I need to slightly modify another similar function already in place so please don't be too upset with me!!
And here's the echoed javascript function:
echo '<script type="text/javascript">';
echo 'function test()';
echo '{';
echo 'alert("hello world");';
echo '}';
echo '</script>';
The javascript is being echoed out at the bottom of the PHP file. I've tried moving it to the top as well but that didn't change anything.
EDIT: Thanks for the replies everyone, all of your advice has been very helpful. I figured out the problem and as many as two things were going wrong. The first problem was that my browser was (I think anyways) caching my javascript so older buggy versions of my function were getting called which was why a perfectly fine function was appearing to create errors. Solved by clearing my cache.
The other problem (I think) was that the javascript function was not, in fact, being loaded before it was being executed. I solved this by putting the javascript in a separate .JS file and linking it to the .PHP file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它似乎在这里工作得很好:
PHP: http://codepad.org/CGY47E1G
HTML: http://jsfiddle.net/maniator/9BCfv/
It seems to be working perfectly fine over here:
PHP: http://codepad.org/CGY47E1G
HTML: http://jsfiddle.net/maniator/9BCfv/
您的代码片段看起来很可靠(尽管我会在回声末尾添加换行符以使结果更具可读性)。您的 JavaScript 中的其他地方可能存在错误,导致您的函数无法加载。
Your code snippets look solid (although I would add newlines to the end of your echos to make the result more readable). You might have an error somewhere else in your javascript that's preventing your function from being loaded.
您应该使用 HEREDOC 来生成该 javascript。它省去了您转义所有内部引号的麻烦。如果您不插入 PHP 变量,则可以使用 NOWDOC 变体,这可以节省一点点 PHP 编译器的时间开销。
更容易阅读...更好的是退出 echo 块的 PHP 模式。任何像样的语法突出显示编辑器都将能够识别 JavaScript 并对其进行适当的着色。
You should be using HEREDOCs to produce that javascript. It saves you the trouble of escaping all the internal quotes. If you're not interpolating PHP variables, you can use the NOWDOC variant, which saves you a teensy bit of PHP compiler overhead.
Far easier to read... Even better would be to drop out of PHP mode for the echo block. Any decent syntax highlighting editor will then be able to recognize the javascript and color it appropriately.