Showing ajax call result in bootstrap modal

断了今生、忘了曾经 提交于 2019-12-06 04:47:25

问题


I need to show multiple data in a Bootstrap modal. For that what I did was this:

js file:

$('#seeProfile').on('show', function() {

  $('.see-user').on('click', function(e) {
  e.preventDefault();

  var id = $(this).data('id');

  $.ajax({
     url:  'getUser',
     type: 'POST',
     data: {id: id},
     success: function(html){
       $('#seeProfile .modal-body .p').html('test');
     },
     error: function(){
       alert("error");
     }  
  });  
});
});      

view snippet (modal):

<div id="seeProfile" class="modal hide fade" tabindex="-1" data-replace="true">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
    <h3>Perfil de Usuario</h3>
</div>

<div class="modal-body">
    <p></p>
</div>
<div class="modal-footer">
    <button type="button" data-dismiss="modal" class="btn">Close</button>
</div>

This is not working. Modal is not showing up, but when inspecting this no errors appear. What am I doing wrong?

EDIT

Given the answers I have realized I missed a dot, so success function should be like:

$('#seeProfile .modal-body p').html("test");

Now That modal is working I need to know how to "insert" data into this new modal organization:

<div id="seeProfile" class="modal hide fade" tabindex="-1" data-replace="true">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
    <h3>Perfil de Usuario</h3>
</div>
<div class="modal-body">
    <div class="scroller" style="height:300px" data-always-visible="1" data-rail-visible="1">
        <div class="row-fluid">
            <div class="span6">
                <h5>Usuario</h5>
                <p><input type="text" class="span12 m-wrap" id="username" readonly></p>
                <h5>Nombre</h5>
                <p><input type="text" id="name" class="span12 m-wrap" value="" readonly></p>
            </div>
            <div class="span6">
                <h5>Email</h5>
                <p><input type="text" class="span12 m-wrap" readonly></p>
            </div>
        </div>
    </div>
</div>
<div class="modal-footer">
    <button type="button" data-dismiss="modal" class="btn">Close</button>
</div>

as you can see there are many "< p >" tags inside modal-body. I have tried inserting an id to it so it can be distinct but it's not working for me. How can I do this?


回答1:


You are binding the click event when the modal gets shown and you never show the modal so the click handler never gets bound.

You can do something like this:

$('.see-user').on('click', function(e) {
    e.preventDefault();
    var id = $(this).data('id');
    $.ajax({
        url:  'getUser',
        type: 'POST',
        data: {id: id},
        success: function(html){
            $('#seeProfile .modal-body p').html('test');
            $('#seeProfile').modal('show');
        },
        error: function(){
            alert("error");
        }  
    });  
});

If you do want to add the click handler when the modal gets shown, you need to use the appropriate handlers. You can find them here (below Events).




回答2:


You should replace

 $('#seeProfile .modal-body .p').html('test');

By

 $('#seeProfile .modal-body p').html('test');

Because you're are looking for a class called 'p' if you insert a dot before. For a html tag <p></p> you just have to write 'p' in the selector.

And complete with the "$('#seeProfile').modal('show');" to show the modal.




回答3:


Try this (work for me):

This will show the modal:

        $('Your button').on('click',function(e){
            e.preventDefault();
            $('your modal').modal('show');
        });

Then this will bind the modal show event:

( you can use show.bs.modal or shown.bs.modal the only differences are the timing of the event launch)

      $('your modal').on('show.bs.modal', function(e) {
          e.preventDefault();
          //ajax call here!!
      });



回答4:


Try this:

$('#seeProfile .modal-body p').html('test');

Note that i removed . (dot) you had before p.




回答5:


Tried using class name.

$(document).on("click", ".getAndUploadButClass", function() {
    alert("Before laoding the image making ajax call to load the images ");
    $('#picturemyModal').modal('show');
});


来源:https://stackoverflow.com/questions/24289575/showing-ajax-call-result-in-bootstrap-modal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!