jQuery click on Appended Element not working

后端 未结 5 931

I have an array. I am getting data from Array and using it in jQuery Append to list. But when I am clicking on list item its only showing the last element.

var a         


        
5条回答
  •  -上瘾入骨i
    2021-01-29 05:18

    You have to move the event outside for loop

    var array = [[1,2,7],[3,4,8],[5,6,9]];
    for (var i = 0; i < array.length; i++){
        var firstVal = array[i].[0];
        var secondVal = array[i].[1];
        var thirdVal = array[i].[2];
    
        var list = "
    "+ firstVal +"

    " + "

    "+ secondVal +"

    "; $("#myDiv").append(list); } $(document).on('click','.divClass', function(){ alert(thirdVal); })

提交回复
热议问题