dsjk3214 2012-12-14 14:47
浏览 49
已采纳

jQuery函数不使用已加载Ajax的数据

When user clicks a link, it runs $.post to retrieve some data and display it. One of the links pulls a table of sessions with a button next to every session. Here is the code for one of the buttons:

<button class="view_session btn btn-info" id="3f87d50493698d06b9b48abe36cd8fc8">Details</button>

And here is my jQuery function:

$('.view_session').click(function(){
    $('#blanket').show();
    $('#load').show();
    $.post("admin/session.php", { id: $(this).attr('id') }, function(data){
        $('#sort').html(data);
        $('#first').hide();
        $('#load').hide();
        $('#blanket').hide();
    }); 
});

But nothing happens. I put in a Console.log to test and nothing triggered. Is there something I have to do because the button is being created after the page loads initially?

  • 写回答

2条回答 默认 最新

  • dongzhi6146 2012-12-14 14:50
    关注

    If you are dynamically adding the button, .click will not bind to it unless it is already there. You either need to bind after the button is added to the DOM or use delegation, like this:

    $(document).on('click', '.view_session', function () {
    

    (a more specific selector than document would be nice).

    Other than that, the third argument as a function to $.post only occurs on success. You can use the deferred interface methods to check for failure and completion as well:

    $.post(...).then(success).fail(failure).done(complete);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘