这真的很奇怪..
我需要从 PHP 向 jquery 发送几个变量。一个是 INT,另一个是字符串。
当 $a 是 INT 时它工作正常但是当我使用字符串时,我得到这个错误.. Uncaught ReferenceError: testString is not defined
这是我的代码。
<?php $a = 'testString'; ?>
<script type="text/javascript">
var a = <?php echo $a; ?>;
alert(a);
</script>
我假设我需要在变量前添加一个 (int) 或 (string),但我不完全确定如何在我的谷歌/尝试中失败。
有什么想法吗?
最佳答案
您忘记了将 var a
的值设为字符串的引号:
var a = "<?php echo $a; ?>";
您在文档中写入的内容是:
var a = testString;
所以 javascript 正在寻找一个名为 testString
的变量。相反,您希望结果是:
var a = "testString";
因此请确保在 php 写入的内容中包含引号。
关于javascript - Uncaught ReferenceError : testString is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709810/