我正在尝试将 php 数组传递给我的 jQuery 函数,我已经完成并正常工作,但我面临格式问题。以下是详细信息。
默认函数
drawHeroArea : function () {
!verboseBuild || console.log('proton.graphsStats.drawHeroArea()');
if($('#hero-area').length)
proton.graphsStats.graph.Area = Morris.Area({
element: 'hero-area',
data: [
{period: '2010Apr Q1', iphone: 2666, ipad: null, itouch: 2647},
{period: '2010 Q2', iphone: 2778, ipad: 2294, itouch: 2441},
{period: '2010 Q3', iphone: 4912, ipad: 1969, itouch: 2501},
{period: '2010 Q4', iphone: 3767, ipad: 3597, itouch: 5689},
{period: '2011 Q1', iphone: 6810, ipad: 1914, itouch: 2293},
{period: '2011 Q2', iphone: 5670, ipad: 4293, itouch: 1881},
{period: '2011 Q3', iphone: 4820, ipad: 3795, itouch: 1588},
{period: '2011 Q4', iphone: 15073, ipad: 5967, itouch: 5175},
{period: '2012 Q1', iphone: 10687, ipad: 4460, itouch: 2028},
{period: '2012 Q2', iphone: 8432, ipad: 5713, itouch: 1791}
],
xkey: 'period',
ykeys: ['iphone', 'ipad', 'itouch'],
labels: ['iPhone', 'iPad', 'iPod Touch'],
pointSize: 2,
hideHover: 'auto'
});
},
我想在这个函数中传递数据
$LoopD['mydata'] = array();
foreach($tData as $mon=>$val){
$LoopD[] = array(
'period' => $mon,
'iphone' => $val['amber'],
'ipad' => $val['red'],
'itouch' => $val['green'],
);
}
$jsonL = json_encode($LoopD);
Javascript:
var jsonL = $jsonL;
$(document).ready(function(e) {
drawHeroArea(jsonL);
});
我正在将数据传递给函数
drawHeroArea : function (jsonL) {
!verboseBuild || console.log('proton.graphsStats.drawHeroArea()');
if($('#hero-area').length)
proton.graphsStats.graph.Area = Morris.Area({
element: 'hero-area',
data: [
jsonL
],
xkey: 'period',
ykeys: ['iphone', 'ipad', 'itouch'],
labels: ['iPhone', 'iPad', 'iPod Touch'],
pointSize: 2,
hideHover: 'auto'
});
}
到目前为止我已经尝试过,但找不到结果,我想要在 jquery 函数中使用数据民歌格式。
{period: '2010 Q2', iphone: 2778, ipad: 2294, itouch: 2441}
最佳答案
jsonL
已经是一个数组,因此在您的 drawHeroArea
中而不是这样:
data: [
jsonL
],
使用这个:
data: jsonL
还要确保替换它:
$LoopD['mydata'] = array();
这样:
$LoopD = array();
关于javascript - 如何将 php 数组 block 传递给 jquery 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30543036/