Passing data to a bootstrap modal

后端 未结 12 1914
心在旅途
心在旅途 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:56

    Here's how I implemented it working from @mg1075's code. I wanted a bit more generic code so as not to have to assign classes to the modal trigger links/buttons:

    Tested in Twitter Bootstrap 3.0.3.

    HTML

    Open Modal
    

    JAVASCRIPT

    $(document).ready(function() {
    
      $('a[data-toggle=modal], button[data-toggle=modal]').click(function () {
    
        var data_id = '';
    
        if (typeof $(this).data('id') !== 'undefined') {
    
          data_id = $(this).data('id');
        }
    
        $('#my_element_id').val(data_id);
      })
    });
    

提交回复
热议问题