将Php数组传递到外部Javascript文件 [英] Pass Php Arrays to External Javascript File
本文介绍了将Php数组传递到外部Javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到了有关将PHP数组传递到外部JavaScript文件的一些问题,但是我听不懂.我知道如何将PHP数组传递给内部JavaScript文件,但不知道如何将它们传递给外部JavaScript文件.
I've seen some of the questions about the passing PHP arrays to an external JavaScript file, but I can't understand it. I know how to pass PHP arrays to internal JavaScript files but not how to pass them to external JavaScript files.
编码
<?php
$a = array("Apple","Orange","Grape");
?>
<script type="text/javascript">
var jArray= <?php echo json_encode($a); ?>;
for(var i=0;i<3;i++){
alert(jArray[i]);
}
推荐答案
使用此代码, JS文件(test.js)
for(var i=0;i<jArray.length;i++){
alert(jArray[i]);
}
PHP文件(test.php)
<?php
$a = array("Apple","Orange","Grape");
?>
<script type="text/javascript">var jArray =<?php echo json_encode($a); ?>;</script>
<script type="text/javascript" src="test.js"></script>
这篇关于将Php数组传递到外部Javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文