Send parameter to Bootstrap modal window?

前端 未结 4 1842
北荒
北荒 2020-12-13 04:39

I have a problem, I cannot pass any parameters to a modal window (using Bootstrap 3), I tried using the solution stated in this link, but I cannot make it work:

Dyna

相关标签:
4条回答
  • 2020-12-13 05:22

    First of all you should fix modal HTML structure. Now it's not correct, you don't need class .hide:

    <div id="edit-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
                <div class="modal-body edit-content">
                    ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
    

    Then links should point to this modal via data-target attribute:

    <a href="#myModal" data-toggle="modal" id="1" data-target="#edit-modal">Edit 1</a>
    

    Finally Js part becomes very simple:

        $('#edit-modal').on('show.bs.modal', function(e) {
    
            var $modal = $(this),
                esseyId = e.relatedTarget.id;
    
            $.ajax({
                cache: false,
                type: 'POST',
                url: 'backend.php',
                data: 'EID=' + essayId,
                success: function(data) {
                    $modal.find('.edit-content').html(data);
                }
            });
        })
    

    Demo: http://plnkr.co/edit/4XnLTZ557qMegqRmWVwU?p=preview

    0 讨论(0)
  • 2020-12-13 05:23

    There is a better solution than the accepted answer, specifically using data-* attributes. Setting the id to 1 will cause you issues if any other element on the page has id=1. Instead, you can do:

    <button class="btn btn-primary" data-toggle="modal" data-target="#yourModalID" data-yourparameter="whateverYouWant">Load</button>
    
    <script>
    $('#yourModalID').on('show.bs.modal', function(e) {
      var yourparameter = e.relatedTarget.dataset.yourparameter;
      // Do some stuff w/ it.
    });
    </script>
    
    0 讨论(0)
  • 2020-12-13 05:25

    I found the solution at: Passing data to a bootstrap modal

    So simply use:

     $(e.relatedTarget).data('book-id'); 
    

    with 'book-id' is a attribute of modal with pre-fix 'data-'

    0 讨论(0)
  • 2020-12-13 05:33

    I have found this better way , no need to remove data , just call the source of the remote content each time

    $(document).ready(function() {
        $('.class').click(function() {
            var id = this.id;
            //alert(id);checking that have correct id
            $("#iframe").attr("src","url?id=" + id);
            $('#Modal').modal({ 
                show: true 
            });
        });
    });
    
    0 讨论(0)
提交回复
热议问题