Passing data to a bootstrap modal

后端 未结 12 1937
心在旅途
心在旅途 2020-11-21 13:10

I\'ve got a couple of hyperlinks that each have an ID attached. When I click on this link, I want to open a modal ( http://twitter.github.com/bootstrap/javascript.html#modal

12条回答
  •  深忆病人
    2020-11-21 13:57

    I find this approach useful:

    Create click event:

    $( button ).on('click', function(e) {
        let id = e.node.data.id;
    
        $('#myModal').modal('show', {id: id});
    });
    

    Create show.bs.modal event:

    $('#myModal').on('show.bs.modal', function (e) {
    
         // access parsed information through relatedTarget
         console.log(e.relatedTarget.id);
    
    });
    

    Extra:

    Make your logic inside show.bs.modal to check whether the properties are parsed, like for instance as this: id: ( e.relatedTarget.hasOwnProperty( 'id' ) ? e.relatedTarget.id : null )

提交回复
热议问题