简体   繁体   English

jQuery函数不适用于已由Ajax加载的数据

[英]jQuery function not working with data that has been loaded with Ajax

When user clicks a link, it runs $.post to retrieve some data and display it. 当用户单击链接时,它将运行$.post检索一些数据并显示它。 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: 这是我的jQuery函数:

$('.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. 我放入Console.log进行测试,但未触发任何操作。 Is there something I have to do because the button is being created after the page loads initially? 我必须做些什么,因为最初在页面加载后就创建了按钮吗?

If you are dynamically adding the button, .click will not bind to it unless it is already there. 如果要动态添加按钮,则.click不会绑定到该按钮,除非该按钮已经存在。 You either need to bind after the button is added to the DOM or use delegation, like this: 将按钮添加到DOM 之后 ,您需要绑定或使用委托,如下所示:

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

(a more specific selector than document would be nice). (比document更具体的选择器会很好)。

Other than that, the third argument as a function to $.post only occurs on success. 除此之外,第三个参数作为$.post函数仅在成功时出现。 You can use the deferred interface methods to check for failure and completion as well: 您也可以使用延迟接口方法来检查失败和完成情况:

$.post(...).then(success).fail(failure).done(complete);

Is there something I have to do because the button is being created after the page loads initially? 我必须做些什么,因为最初在页面加载后就创建了按钮吗?

Yes. 是。

The reason for this is that handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the binding occurs. 这样做的原因是将处理程序附加到jQuery对象中当前选择的元素上,因此这些元素必须在绑定发生时存在。

The solution is that you either have to re-bind the event manually to the element ones it has been added or bind the event using delegation. 解决方案是您必须将事件手动重新绑定到已添加的元素上,或者使用委托绑定事件。

For jQuery 1.7 or later use on() with delegation: 对于jQuery 1.7或更高版本,将on()与委托一起使用:

$("#YourclosestStaticElement").on("click", ".view_session", function () {
   //... same code as before
}

For jQuery 1.4.3 up-to/including 1.6 use delegate() : 对于jQuery 1.4.3之前的版本(包括1.6),请使用delegate()

$("#YourclosestStaticElement").delegate(".view_session", "click", function () {
   //... same code as before
}

For jQuery 1.3 up-to/including 1.4.3 live() would be the only way, aside from re-binding manually: 对于jQuery 1.3,除了手动重新绑定之外,只有live()包括1.4.3) live()才是唯一的方法:

$(".view_session").live("click", function () {
   //... same code as before
}

In addition there is a whole lot of reasons why live() was depracated, among the fact that it always bubbles up to the document before triggering the event, meaning stopPropogation() can become an issue for example. 另外,有很多原因导致live()被取消使用,原因是它总是在触发事件之前冒泡到文档中,这意味着stopPropogation()可能成为一个问题。

The whole list of reasons is also in the live() documentation. 全部原因列表也位于live()文档中。

To quote from the documentation : 引用文档

  • $(selector).live(events, data, handler); $(selector).live(事件,数据,处理程序); // jQuery 1.3+ // jQuery 1.3+
  • $(document).delegate(selector, events, data, handler); $(document).delegate(选择器,事件,数据,处理程序); // jQuery 1.4.3+ // jQuery 1.4.3+
  • $(document).on(events, selector, data, handler); $(document).on(事件,选择器,数据,处理程序); // jQuery 1.7+ // jQuery 1.7+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM