得到反对的顺序

时间:2012-12-27 23:48:52

标签: jquery arrays click each

检查此代码:

var test = new Array();

//I need each() to get the data from every row
$(".quiz_list_row").each(function(index){

    // Gets the data necessary from HTML5 tag data
    $quiz_list_id = $(this).data("quizlistId");
    $quiz_level_reached = $(this).data("quizlevelReached");

    test [0] = new Array($quiz_list_id,$quiz_level_reached);

    $(this).click(function(){
    alert("test: "+test.toString());
    });
)};

div就像这样(这是正确的):

<div class="quiz_list_row" data-quizlist-id="1" data-quizlevel-reached="5">
     <div class="inline" id="quiz_list_cell_id1">Quiz 1</div>
     <div class="inline" id="quiz_list_cell_id2">Current level: 5</div>
</div>
<div class="quiz_list_row" data-quizlist-id="2" data-quizlevel-reached="7">
     <div class="inline" id="quiz_list_cell_id1">Quiz 2</div>
     <div class="inline" id="quiz_list_cell_id2">Current level: 7</div>
</div>

所以,当我点击第一个div,它应该在第一个警报上显示“1,5”而在第二个警告上显示“2,7”时,我会反过来。

2 个答案:

答案 0 :(得分:0)

你在循环的每次传递中都在写test[0]

尝试将test[0]=替换为:

test.push([$quiz_list_id,$quiz_level_reached])

答案 1 :(得分:0)

以下是我所做的更改:

var test = new Array();

$(".quiz_list_row").each(function(index){
        // Gets the data necessary to show game chosen
        $quiz_list_id = $(this).data("quizlistId");
        $quiz_level_reached = $(this).data("quizlevelReached");

            test.push($quiz_list_id,$quiz_level_reached);

        $(this).click(function(){
            alert("test: "+test);
        });

});

div:

<div class="quiz_list_row" data-quizlist-id="1" data-quizlevel-reached="5">
    <div class="inline quiz_list_cell" id="quiz_list_cell_row0_id1">Quiz 1</div>
    <div class="inline quiz_list_cell" id="quiz_list_cell_row0_id2">Current level: 5</div>
</div>
<div class="quiz_list_row" data-quizlist-id="2" data-quizlevel-reached="7">
    <div class="inline quiz_list_cell" id="quiz_list_cell_row1_id1">Quiz 2</div>
    <div class="inline quiz_list_cell" id="quiz_list_cell_row1_id2">Current level: 7</div>
</div>

积分是在评论上,好吗?