简体   繁体   English

找不到这段代码有什么问题

[英]Can't find what's wrong in this code

I have the following script which brings up a dialog on my screen telling me there has been an error: 我有以下脚本,该脚本在屏幕上弹出一个对话框,告诉我出现错误:

<script type="text/javascript">
var myFunc = function() 
{
    var html='<div class="cs-body">explanation of the error</div>';
    $(html).csDialog();
    return false;
};
</script>

I want this dialog to pop-up when my php script returns a "1" value for $error, like this: 我希望当我的PHP脚本为$ error返回“ 1”值时弹出此对话框,如下所示:

if ($error==1) {
echo "<script type='javascript'>$(document).ready(myFunc)</script>";
}

Even if I leave the if-clause and just echo the script it doensn't do anything. 即使我离开if子句并仅回显脚本,它也不会做任何事情。 Can somebody tell me what I'm missing here? 有人可以告诉我我在这里想念的吗?

Thanks in advance! 提前致谢!

try changing script type from 'javascript' to 'text/javascript' 尝试将脚本类型从“ javascript”更改为“ text / javascript”

that did the trick! 做到了! thanks lostsource! 谢谢lostsource!

您需要确保在包含jQuery 之后输出此脚本片段。

Check if you javascript function is defined when running this 检查运行此程序时是否定义了javascript函数

echo "<script type='javascript'>$(document).ready(myFunc)</script>

Include jQuery and your js function in the 将jQuery和js函数包括在 tag, just to see if it works. 标签,只是看它是否有效。 PS: you should only add js script in the PS:您只应在其中添加js脚本 tag if they are necessary right away, else, added them at the bottom of the 标签(如果有必要的话),否则,请将其添加到 tag. 标签。 Remember, the loading of js files blocks the loading of any other page resources 请记住,加载js文件会阻止加载其他任何页面资源

try this 尝试这个

<?php if ($error==1) {  ?>
 <script type="text/javascript">
   $(document).ready(myFunc)
 </script>";

<?php } ?>

Dins 丁斯

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

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