2013-03-16 122 views
2

我试图在输入页面时将输入字段的数据输入到简单的jqPlot图表。将变量传递给jqPlot图表

使用标准语法,jqPlot工作正常。例如:

var plot1 = $.jqplot ('chart1', [[1,2,3,4,]]); 

使用变量会导致图表最多只响应变量中的第一个整数。我想我很接近,并怀疑这是Var的数值/字符串质量的问题。

我在这里的钱还是我想做的事情是不可能的?

HTML:

<div class="dataforchart"> <!-- VALUES --> 
<input type="text" value="5"> 
<input type="text" value="2"> 
<input type="text" value="8"> 
<input type="text" value="1"> 
</div> 
<div id="chart1"> <!-- CHART --> 
</div> 

脚本:在SO

$('.dataforchart').each(function(){ 

str = ''; 
$(this).children("input").each(function() { 
str = str + ', ' + $(this).val(); 
}); 

alert(str); 
var plot1 = $.jqplot ('chart1', [[str]]); 

}); 

我发现非常类似的问题,但我还没有找到一个有效的解决方案。

更新:我做了一个演示,如果有人想测试:http://jsfiddle.net/fdKRw/3/

回答

2

它是一个数组,而不是字符串,所以只要给它点阵列:

... 
var points = []; 
$(this).children("input").each(function(index) { 
    points[index] = $(this).val(); 
}); 

var plot1 = $.jqplot('chart1',[points]); 

更新小提琴:http://jsfiddle.net/fdKRw/4/

+0

谢谢你,那太棒了。 – 2013-03-16 04:28:22