我在这个网站上看过很多关于这个问题的解决方案。然而,我尝试的一切似乎都不起作用。
在我的php文件中,有这样一个代码:
<?php
$monthlycalories = "[1, 1364, 1052, 922, 1, 1, 10, 1, 10, 10, 10, 265]";
?>
<script type="text/javascript">
var test = "<?php echo $monthlycalories; ?>";
</script>
在我的 JavaScript 文件中,我有以下代码:
$(function () {
var example = test;
alert(example);
$('#monthly').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Monthly Calorie Intake'
},
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
},
yAxis: {
min: 0,
title: {
text: 'Calories'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0"></td>' +
'<td style="padding:0"><b>{point.y:.1f} Calories</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'Months',
data: example
}]
});
});
Javascript是一种图表。我已经能够将信息传递到Javascript文件中。我已经使用了
alert()
函数,并且可以看到它确实将变量带过来了。但是,当我在Javascript中使用example
变量时,它不起作用。此外,如果我复制alert结果并将data: example
替换为data: [1, 1364, 1052, 922, 1, 1, 10, 1, 10, 10, 10, 265]
,则它可以工作。我无法理解为什么变量
example
不起作用,但手动输入信息后,图表可以正常工作?