如何根据点击获取数组

时间:2017-06-26 20:42:11

标签: jquery

我的结构如下。我想要做的是,如果用户点击其中一个div并通过将变量第一部分(标准)与索引相结合来检索数组,但我似乎无法实现这一点(在$ .each之后) (...

<div class="selections">
   <div id="select1"></div>
   <div id="select2"></div>
   <div id="select3"></div>
   <div id="select4"></div>
   <div id="select5"></div>
</div>

$('.selections div').on('click', function (index) {
    var numbered = $(this).index() + 1;
    var selectedSelect = $('#select' + numbered);
    $.each((standard + numbered), function (index, value) {
        if (index == numbered) {
            //code
        }
    })
    $('#select' + numbered).addClass("selectionBoxActive");
})

var standard1 = [
    { text: "Expertise 1", abbreviation: "AA" },
    { text: "Expertise 2", abbreviation: "AA" },
    { text: "Expertise 3", abbreviation: "AA" },
    { text: "Expertise 4", abbreviation: "AA" },
    { text: "Expertise 5", abbreviation: "AA" }
];
var standard2 = [
    { text: "Expertise 1", abbreviation: "BB" },
    { text: "Expertise 2", abbreviation: "BB" },
    { text: "Expertise 3", abbreviation: "BB" },
    { text: "Expertise 5", abbreviation: "BB" }
];
var standard3 = [
    { text: "Expertise 2", abbreviation: "CC" },
    { text: "Expertise 3", abbreviation: "CC" },
    { text: "Expertise 4", abbreviation: "CC" },
    { text: "Expertise 5", abbreviation: "CC" }
];
var standard4 = [
    { text: "Expertise 1", abbreviation: "DD" },
    { text: "Expertise 2", abbreviation: "DD" },
    { text: "Expertise 3", abbreviation: "DD" },
    { text: "Expertise 4", abbreviation: "DD" },
    { text: "Expertise 5", abbreviation: "DD" }
];
var standard5 = [
    { text: "Expertise 1", abbreviation: "EE" },
    { text: "Expertise 2", abbreviation: "EE" },
    { text: "Expertise 3", abbreviation: "EE" },
    { text: "Expertise 4", abbreviation: "EE" },
    { text: "Expertise 5", abbreviation: "EE" }
];

1 个答案:

答案 0 :(得分:1)

我不相信你可以做你正在尝试使用你的数组。我建议将它们转换为多维数组。

$('.selections div').on('click', function (index) {
    var numbered = $(this).index();
    var selectedSelect = $('#select' + numbered);
    $.each(arr[numbered], function (index, value) {
        if (index == numbered) {
            //code
            console.log(arr[numbered][index]);
        }
    })
    $('#select' + (numbered+1)).addClass("selectionBoxActive");
})
var arr = [[
    { text: "Expertise 1", abbreviation: "AA" },
    { text: "Expertise 2", abbreviation: "AA" },
    { text: "Expertise 3", abbreviation: "AA" },
    { text: "Expertise 4", abbreviation: "AA" },
    { text: "Expertise 5", abbreviation: "AA" }
],[
    { text: "Expertise 1", abbreviation: "BB" },
    { text: "Expertise 2", abbreviation: "BB" },
    { text: "Expertise 3", abbreviation: "BB" },
    { text: "Expertise 5", abbreviation: "BB" }
],[
    { text: "Expertise 2", abbreviation: "CC" },
    { text: "Expertise 3", abbreviation: "CC" },
    { text: "Expertise 4", abbreviation: "CC" },
    { text: "Expertise 5", abbreviation: "CC" }
],[
    { text: "Expertise 1", abbreviation: "DD" },
    { text: "Expertise 2", abbreviation: "DD" },
    { text: "Expertise 3", abbreviation: "DD" },
    { text: "Expertise 4", abbreviation: "DD" },
    { text: "Expertise 5", abbreviation: "DD" }
],[
    { text: "Expertise 1", abbreviation: "EE" },
    { text: "Expertise 2", abbreviation: "EE" },
    { text: "Expertise 3", abbreviation: "EE" },
    { text: "Expertise 4", abbreviation: "EE" },
    { text: "Expertise 5", abbreviation: "EE" }
]];
.selectionBoxActive{
  border: 1px solid orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="selections">
   <div id="select1">1</div>
   <div id="select2">2</div>
   <div id="select3">3</div>
   <div id="select4">4</div>
   <div id="select5">5</div>
</div>