Passing data to a bootstrap modal

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

    <a href="#" data-target="#my_modal" data-toggle="modal" data-id="my_id_value">Open Modal</a>
    

    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);
      })
    });
    
    0 讨论(0)
  • 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 )

    0 讨论(0)
  • 2020-11-21 14:00

    Your code would have worked with correct modal html structure.

    $(function(){
      $(".open-AddBookDialog").click(function(){
         $('#bookId').val($(this).data('id'));
        $("#addBookDialog").modal("show");
      });
    });
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    
    
    <a data-id="@book.Id" title="Add this item" class="open-AddBookDialog">Open Modal</a>
    
    <div id="addBookDialog" class="modal fade" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-body">
           <input type="hidden" name="bookId" id="bookId" value=""/>
          </div>
        
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    </html>

    0 讨论(0)
  • 2020-11-21 14:02

    Bootstrap 4.3 - use code from below snippet - more here

    $('#exampleModal').on('show.bs.modal', function (event) {
      let bookId = $(event.relatedTarget).data('bookid') 
      $(this).find('.modal-body input').val(bookId)
    })
    a.btn.btn-primary.open-AddBookDialog {color: white }
    <!-- Initialize Bootstrap 4 -->
    
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    
    <!-- BUTTON -->
    
    <a 
      class="btn btn-primary open-AddBookDialog" 
      data-toggle="modal" 
      data-target="#exampleModal" 
      data-bookid="@book.Id" 
      title="Add this item"
    >Open</a>
    
    <!-- MODAL -->
    
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
        
          <div class="modal-body">
            <input type="hidden" name="bookId" id="bookId" value=""/>
            <input type="text" class="form-control" id="recipient-name">    
          </div>
          
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          </div>
    
        </div>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-21 14:02

    You can try simpleBootstrapDialog. Here you can pass title, message, callback options for cancel and submit etc...

    0 讨论(0)
  • 2020-11-21 14:07

    Here is a cleaner way to do it if you are using Bootstrap 3.2.0.

    Link HTML

    <a href="#my_modal" data-toggle="modal" data-book-id="my_id_value">Open Modal</a>
    

    Modal JavaScript

    //triggered when modal is about to be shown
    $('#my_modal').on('show.bs.modal', function(e) {
    
        //get data-id attribute of the clicked element
        var bookId = $(e.relatedTarget).data('book-id');
    
        //populate the textbox
        $(e.currentTarget).find('input[name="bookId"]').val(bookId);
    });
    

    http://jsfiddle.net/k7FC2/

    0 讨论(0)
提交回复
热议问题