【问题标题】:Append multiple objects within an Array with jQuery使用 jQuery 在数组中追加多个对象
【发布时间】:2017-10-21 15:08:30
【问题描述】:

我是学习 jQuery 的学生,我的目标是使用 optionArray 索引中的 4 个对象将数组中的每个对象附加到 4 个不同的按钮。目前它只附加 optionArray 的索引 i 的单个按钮,如下所示:Radio Head、Gorillaz、Coldplay、Arctic Monkeys。

我希望它在 optionsArray 的索引 i 中单独附加每个对象作为 4 个不同的按钮。例如:

  1. 收音机头
  2. 大猩猩
  3. 酷玩游戏
  4. 北极猴

我不确定如何执行此操作...感谢您提供的任何高级帮助。

JS:

window.onload = function() {
    $('#start').html('<div class="text-center"><button type="button" class="btn btn-default">Start</button></div>');
};

var questionArray = ["This bands second album went platinum 5 times in the UK and double Platinum in the US."];
var optionArray = [["Radio Head", "Gorillaz", "Coldplay", "Arctic Monkeys"], []];
var answerArray= ["Gorillaz"];
var imageArray= ["http://cdn3.pitchfork.com/artists/1767/m.65d9c64d.jpg", "http://crowningmusic.com/storage/rA7GUFFoBCtT8Jg4L1tv.png", "", "", ""];

var count = 0;

$("#start").on('click', function() {
    $(this).css("display","none");
    for (var i = 0; i < questionArray.length; i++) {
        $("#question").html(questionArray[i]);
        for (var j = 0; j < 4; j++) {
            $("#options").append("<button>" + optionArray[i] + "</button>");
        }
    }   
    // $("#holder").html("<img src=" + questionArray[count] + ">");
});

【问题讨论】:

  • 您的目标很难理解“我希望它将索引 i 中的每个索引附加为 4 个不同的按钮。”
  • 这是您要找的吗? jsfiddle.net/36zck6dk

标签: javascript jquery arrays append


【解决方案1】:

您正在使用二维数组作为选项。将选项的 for 循环迭代更改为此

    for (var j = 0; j < 4; j++) {
       $("#options").append("<button>" + optionArray[i][j] + "</button>");
    }

【讨论】:

  • 太棒了,谢谢!这正是我所希望的。
猜你喜欢
  • 2018-01-04
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多