-1

检查此代码:

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”,我得到相反的结果。

4

2 回答 2

0

test[0]在循环的每一次都写完了

尝试替换test[0]=为:

test.push([$quiz_list_id,$quiz_level_reached])
于 2012-12-27T23:56:42.320 回答
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>

学分在评论中,好吗?

于 2012-12-28T08:33:40.040 回答