简体   繁体   English

在Ajax调用之后重新绑定部分视图元素

[英]Rebind Partial View Elements after Ajax call

$('#approveRequest').on('click', function () {
        $.ajax({
            url: $('#approveRequest').data('url'),
            type: 'post',
            success: function(result) {
                $('#accountGroupAdmin').html(result);
            }
    });
    });

How do I rebind the elements are the partial view is refreshed ? 如何重新绑定部分视图刷新的元素?

You can use event delegation with jquery.on . 您可以将事件委托jquery.on一起使用 The static parent should remain during refresh. 刷新期间应保留静态父级。

$('staticParentIdCouldBeBody').on('click', '#approveRequest', function () {
        $.ajax({
            url: $('#approveRequest').data('url'),
            type: 'post',
            success: function(result) {
                $('#accountGroupAdmin').html(result);
            }
    });
});

Delegated events 委托活动

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. 委派事件的优势在于,它们可以处理来自后代元素的事件,这些事件在以后的时间添加到文档中。 By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers 通过选择保证在附加委托事件处理程序时会出现的元素,您可以使用委托事件来避免频繁附加和删除事件处理程序的需要。

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

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