2013-03-16 53 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