在引导模态中显示ajax调用结果

Lim*_*mon 4 javascript ajax jquery twitter-bootstrap bootstrap-modal

我需要在Bootstrap模式下显示多个数据。为此,我要做的是:

js文件:

$('#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");
     }  
  });  
});
});      
Run Code Online (Sandbox Code Playgroud)

查看摘要(模式):

<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>
Run Code Online (Sandbox Code Playgroud)

这是行不通的。模态没有显示,但是检查时不会出现错误。我究竟做错了什么?

编辑

给定我已经意识到的答案,我错过了一个点,因此成功函数应该类似于:

$('#seeProfile .modal-body p').html("test");
Run Code Online (Sandbox Code Playgroud)

现在该模式正在工作,我需要知道如何将数据“插入”到这个新的模式组织中:

<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>
Run Code Online (Sandbox Code Playgroud)

如您所见,模态体内有许多“ <p>”标签。我尝试在其中插入一个id,这样它可以与众不同,但对我不起作用。我怎样才能做到这一点?

A1r*_*Pun 5

当模态被显示时,您将绑定click事件,而您从不显示模态,因此单击处理程序将不会被绑定。

您可以执行以下操作:

$('.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");
        }  
    });  
});
Run Code Online (Sandbox Code Playgroud)

如果确实要在显示模式时添加单击处理程序,则需要使用适当的处理程序。您可以在此处(活动下方)找到它们