简体   繁体   English

在jQuery SwitchCase中回显PHP变量?

[英]Echo PHP variable inside jQuery SwitchCase?

I have a jQuery script that I am including on a PHP page that takes mt content and creates facebook/myspace share links. 我在包含mt内容并创建facebook / myspace共享链接的PHP页面上包含一个jQuery脚本。 I am having trouble echoing PHP code inside of the swith case statement that builds the share URL's. 我在构建共享URL的swith case语句中回显PHP代码时遇到麻烦。 Here's my code: 这是我的代码:

case 'facebook': this.href += '?t=' + document.title + '&u=http://foobar.com/detail.php?id='; 案例'facebook':this.href + ='?t ='+ document.title +'&u = http://foobar.com/detail.php?id ='; break; 打破;

The server isn't parsing the PHP code correctly, it's passing the value id=<?php echo "1";?> rather than 1. Can I not insert PHP code into a javascript include? 服务器未正确解析PHP代码,而是传递值id=<?php echo "1";?>而不是1。我不能将PHP代码插入javascript include吗?

Option 1. In your file.php: 选项1.在您的file.php中:

<script type="text/javascript">
   var varName = "<?php echo '1'; ?>"; // global
</script>
<script type="text/javascript" src="myscript.js"></script>

Option 2. create a PHP file that returns the javascript content type externalphp 选项2。创建一个返回javascript内容类型externalphp的PHP文件

By default, apache with mod_php doesn't parse files with a .js extension. 默认情况下,带有mod_php的apache不会解析扩展名为.js的文件。 That behaviour can be modified in the apache config files. 可以在apache配置文件中修改该行为。

Alternatively (this solution will be less heavy on the parser because it wont parse all .js files), give the javascript files you need to parse a .php extension 或者(此解决方案在解析器上不会很繁琐,因为它不会解析所有 .js文件),提供解析.php扩展名所需的javascript文件

Have you tried putting it in a variable instead ? 您是否尝试过将其放在变量中?

var varName = '<?php echo "1"; ?>' ; or var varName = "<?php echo '1'; ?>";

and then use that variable in your javascript code. 然后在您的JavaScript代码中使用该变量。

You may need to use praseInt(...) to get a number through. 您可能需要使用praseInt(...)来获取数字。

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

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