问题描述
|
我有一个返回2d数组的函数。我将如何显示其中的每个元素?我的代码看起来像这样:
$.post(\"/Question/GetPollQuestionsForView/\",{ poll_ID: pollId },function(result) {
//$(\"#CustomerList tbody\").append($(result));
var myarray = new Array()
myarray = result;
alert(myarray);
});
警报返回的是“ 1”。我该如何将数组中的每个值附加到名为#divComparativeQuestions
的div标签中。
解决方法
例如:
var data = new Array();
for(var i=0;i<myarray.length;i++){
data.push(myarray[i].join(\',\'));
}
$(\'#divComparativeQuestions\').html(data.join(\'<br/>\'));
(希望此方法有效,未经测试:),但是您知道了)
, 我猜你想要这样的东西:
// given an array [[1,2],[3,4]] with a desired result of <div>1234</div>
$.post(\"/Question/GetPollQuestionsForView/\",{ poll_ID: pollId },function(data) {
if(data) {
var div = $(\'#divComparativeQuestions\');
$.each(data,function(index,element) {
div.append(element); // will be inner array of [1,2] or [3,4]
});
}
});
所有非常基本的东西,在这种情况下,我利用了js通常将数组展平为字符串而没有逗号的事实,以得到所需的输出,但是如果您想以某种方式对它们进行定界或将它们包装在标签中或其他任何东西, \花费几秒钟浏览http://docs.jquery.com即可轻松解决问题