2013-11-26 41 views
0

我有一个对象window.Think和数组:调用JavaScript对象数组中的jQuery

window.Think.questions = [ 
    {answers: [ 
    {a: 1, b: 2} 
    ]} 
]; 

我想这个数组的.append()部分插入我的HTML元素.buttons

我该如何修改它以便打印我的数组的一部分?

$think = window.Think.new; 

$(document).ready(function(){ 
    $(".buttons").append($think.questions[1]); 
}); 
+1

你必须将数组串化,或者迭代创建文本/ html。 –

+2

你想追加什么格式? – charlietfl

+0

我想取1并将其作为复选框的名称。 '' –

回答

1

如果你只是想追加

A是:1 - B为:2

这个确切的数据结构,你会怎么做:

$(document).ready(function(){ 
    $think = window.Think.questions; 
    $(".buttons").append("A is: " + $think[0].answers[0].a + " -- B is: " +$think[0].answers[0].b); 
}); 

编辑:

我刚才看到这里上面...您的评论是你需要做什么:

$(document).ready(function(){ 
    $think = window.Think.questions; 
    $(".buttons").append('<input type="checkbox" name="' + $think[0].answers[0].a + '"/>'); 
    $(".buttons").append('<input type="checkbox" name="' + $think[0].answers[0].b + '"/>'); 
}); 

你可能想的name要的东西不仅仅是一个整数不同的...你确定你不是说value

1

由于@KevinB指出的那样,你将不得不字符串化要在按钮显示的部分,例如:

var answers = window.Think.questions[0].answers[0], 
    values = [answers.a, answers.b].join(' ');// turn the array values into a space delimited string 

$('.buttons').append(values) 
1

假设你的复选框,有一个ID:

$('#cheboxID').attr('name', $think[0].answers[0].a);