Load content with ajax in bootstrap modal

前端 未结 3 605
挽巷
挽巷 2020-11-30 02:42

I am trying to have my bootstrap modal retrieve data using ajax:



        
相关标签:
3条回答
  • 2020-11-30 03:08

    The top voted answer is deprecated in Bootstrap 3.3 and will be removed in v4. Try this instead:

    JavaScript:

    // Fill modal with content from link href
    $("#myModal").on("show.bs.modal", function(e) {
        var link = $(e.relatedTarget);
        $(this).find(".modal-body").load(link.attr("href"));
    });
    

    Html (Based on the official example. Note that for Bootstrap 3.* we set data-remote="false" to disable the deprecated Bootstrap load function):

    <!-- Link trigger modal -->
    <a href="remoteContent.html" data-remote="false" data-toggle="modal" data-target="#myModal" class="btn btn-default">
        Launch Modal
    </a>
    
    <!-- Default bootstrap modal example -->
    <div class="modal fade" id="myModal" 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-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
          </div>
          <div class="modal-body">
            ...
          </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>
    

    Try it yourself: https://jsfiddle.net/ednon5d1/

    0 讨论(0)
  • 2020-11-30 03:09

    Here is how I solved the issue, might be useful to some:

    Ajax modal doesn't seem to be available with boostrap 2.1.1

    So I ended up coding it myself:

    $('[data-toggle="modal"]').click(function(e) {
      e.preventDefault();
      var url = $(this).attr('href');
      //var modal_id = $(this).attr('data-target');
      $.get(url, function(data) {
          $(data).modal();
      });
    });
    

    Example of a link that calls a modal:

    <a href="{{ path('ajax_get_messages', { 'superCategoryID': 6, 'sex': sex }) }}" data-toggle="modal">
        <img src="{{ asset('bundles/yopyourownpoet/images/messageCategories/BirthdaysAnniversaries.png') }}" alt="Birthdays" height="120" width="109"/>
    </a>
    

    I now send the whole modal markup through ajax.

    Credits to drewjoh

    0 讨论(0)
  • 2020-11-30 03:13

    Easily done in Bootstrap 3 like so:

    <a data-toggle="modal" href="remote.html" data-target="#modal">Click me</a>
    
    0 讨论(0)
提交回复
热议问题