当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - how to get data-id and pass it into function?

问题描述:

HTML

<a class="bookmarkit" href="#modalBookmark" data-toggle="modal" role="button" data-id="${image?.id}">Bookmark</a>

$('#modalBookmark').on('shown', function(){

getBookmarks( userId, $(this).data('id'));

});

$('.btn-add-list').click(function(e){

e.preventDefault();

var bookmarkName = $('.des-bookmark-new input[name="list"]').val();

console.log(bookmarkName);

var bookmark = { ownerId: userId, name: bookmarkName };

addBookmark( bookmark );

});

$('.des-bookmark-list-wrapper').on('click', '.des-bookmark-link', function(e) {

e.preventDefault();

console.log('here - add bookmark group');

addBookmarkItem( $(this), $(this).data('id'), $(this).parent().attr('data-bookmark-id') );

});

it will be dynamic id that would be passed to jquery bookmark function to get bookmark value, show count after choosing the bookmark group, etc.

The problem is that $(this).data('id') is not defined.

Any help appreciated!

网友答案:

When you open modal box, this function target modal div not an anchor opener link

$('#modalBookmark').on('shown', function(){
   getBookmarks( userId, $(this).data('id'));
})

You should try this way

<a class="bookmarkit" href="#modalBookmark" data-toggle="modal" role="button" data-id="${image?.id}">Bookmark</a>

And use current anchor selector with shown event

$('#modalBookmark').on('shown', function(){

  getBookmarks( userId, $(".bookmarkit").data('id'));

});

OR with click event

$(".bookmarkit").on('click', function(){
   getBookmarks( userId, $(this).data('id'));
})

hope it would work

网友答案:

Try using $(this).attr('data-id')

分享给朋友:
您可能感兴趣的文章:
随机阅读: