【问题标题】:Create array variable创建数组变量
【发布时间】:2011-04-05 11:14:43
【问题描述】:

我想创建这种输出

var s1 = [['Sony',7],['Samsung',5],['LG',8]];

这样我就可以使用它作为变量传递我的图表

从我的 ajax 的结果中出来

success: function(data){

    //code to extract the data value here

    var s1= need to create the data here

    $.jqplot('chart',[s1],{ blah blah blah

}

成功函数中的“data”返回这个表格布局

<table id="tblResult">
    <tr class="tblRows">
        <td class="clsPhone">Sony</td><td class="clsRating">7</td>
    </tr>
    <tr class="tblRows">
        <td class="clsPhone">Samsung</td><td class="clsRating">5</td>
    </tr>
    <tr class="tblRows">
        <td class="clsPhone">LG</td><td class="clsRating">8</td>
    </tr>
</table>

你能帮我创建这个逻辑吗?

提前致谢

编辑: 我正在寻找类似以下的解决方案:

var s1;
$(".tblRows").each(function(){
    // here I don't know exactly on what to do
    //s1.push($(".clsPhone").text(),$(".clsRating").text()));
});
// all I wanted is to make the resul s1=[['Sony',7],['Samsung',5],['LG',8]];

因为jqplot需要这种参数

s1=[['Sony',7],['Samsung',5],['LG',8]];
$.jqplot('chart',[s1],{
        renderer:$.jqplot.PieRenderer,
        rendererOptions:{
            showDataLabels:true,
            dataLabelThreshold:1
        }
    }
});

所以我正在寻找一种方法来从数据中创建变量 s1 的值 这可能吗?

【问题讨论】:

  • 你想从 HTML 表中创建一个数组,对吗?
  • 是否可以修改返回数据的服务器端?
  • 那么......输入是什么样的?
  • @fabrik、@Ottomanlast、@strager:编辑了我的帖子。希望你们清楚:(

标签: jquery variables multidimensional-array jqplot


【解决方案1】:
var s1 = [];
$(".tblRows").each(function(){
    // create a temp array for this row
    var row = [];
    // add the phone and rating as array elements
    row.push($(this).find('.clsPhone').text());
    row.push($(this).find('.clsRating').text());
    // add the temp array to the main array
    s1.push(row);
});

【讨论】:

  • 我认为您可能需要将 $(data) 传递给初始选择器才能使其工作。 $(".tblRows", $(data)).... 这是因为此时传入的数据不一定在文档中
  • 啊,当我使用变量名 data 时,我没有看到它已经被使用过 - 我已经更改了答案以将其重命名为 row
  • 逻辑对我有用...从数据中获取选择器对我来说不是什么大问题 :) 谢谢亚当
【解决方案2】:

你可以这样做:

var row = [];
$(".tblRows").each(function () {
    row.push([$(this).find('.clsPhone').text(),
              $(this).find('.clsRating').text()]);
});

$.jqplot('chart', [row], {
    //... 
});

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2023-03-05
    • 2021-12-22
    • 2014-01-17
    • 2017-09-23
    • 1970-01-01
    相关资源
    最近更新 更多