2017-06-26 12 views
-1

私は以下のような構造を持っています。私がしようとしているのは、ユーザーがdivの1つをクリックし、変数の最初の部分(標準)をインデックスと結びつけて配列を取得する場合ですが、そのようにすることはできません($ .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

私はあなたの配列にしようとしているものを行うことが可能であるとは思わない。私は多次元配列にそれらを変換することをお勧め。

$('.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>

+0

ええ、私はそれを行うことができます、私はちょうど興味があった – Keith

関連する問題