Binding click event handlers in a loop causing problems in jQuery

前端 未结 3 697
一个人的身影
一个人的身影 2021-01-19 14:49

I am trying to run the following code:

I pass parameter to a function, but it always has the value of the last object run through the loop. I read some articles abou

3条回答
  •  一个人的身影
    2021-01-19 15:41

    Perhaps there's a problem with using parentId in the callback.

    Try alert(parents[i]['PARENT_ID']);.

    EDIT: Let's see if we can get around our scope issues with .data().

    aDisplayCol.data("parentId",parents[i]['PARENT_ID']);
    aDisplayCol.click(function() {
       alert($(this).data('parentId'));
    });
    

提交回复
热议问题