简体   繁体   English

AJAX成功html对话框,单击按钮不会关闭

[英]AJAX success html dialog, won't close on button click

So I've made an AJAX request, it submits fine and returns the HTML on success as expected - but in the HTML that is returned to the target div element, it has a button. 因此,我提出了AJAX请求,它可以很好地提交并按预期成功返回HTML-但是在返回到目标div元素的HTML中,它具有一个按钮。 I've added jQuery to this, that when it's clicked, it is to hide the HTML from the AJAX success. 我已经在其中添加了jQuery,即单击它时将隐藏AJAX成功中的HTML。

In short: I want the close button to close the div, but it doesn't seem to be working. 简而言之:我希望关闭按钮可以关闭div,但似乎不起作用。

$('#user_report__dd #make_report').click(function(){
        var interaction_type = $(this).data('interaction_type');
        var user_id = $(this).data('user_id');
        $.ajax({
            type: 'POST',
            dataType: 'html',
            url: ajax_url,
            data: {
                interaction_type: interaction_type,
                user_id: user_id,
            },
            success:function(html){
                // $('.ajax_call').html(html);
                $('.ajax_call').html(html);
                // stop body from scrolling
            }
        });
    });
if(isset($_POST['interaction_type']) && $_POST['interaction_type'] == 'report_user'){
    global $report;
    $report->Form($_POST['user_id'], 'user');
    // This returns the HTML
}

And then in my main jQuery file 然后在我的主要jQuery文件中

$('.close').click(function(){
        $(this).parents('.pulled_in_html');
    });

Because the element doesn't exist in the DOM when your click event handler is created, it doesn't find the element. 因为创建单击事件处理程序时DOM中不存在该元素,所以找不到该元素。 Instead you can delegate the event from body like below: 相反,您可以从body委派事件,如下所示:

$('body').on('click', '.close', function(){
    $(this).parents('.pulled_in_html');
});

Event delegation allows you to avoid adding event listeners to specific nodes; 事件委托使您可以避免将事件侦听器添加到特定节点; instead, the event listener is added to one parent. 而是将事件侦听器添加到一个父对象。 That event listener analyzes bubbled events to find a match on child elements. 该事件侦听器分析冒泡事件以找到子元素的匹配项。

You can read more about event delegation here 您可以在此处了解有关事件委托的更多信息

Your code doesn't seem to be making any attempt at hiding anything and I don't know if you left this out on purpose or not, but to hide the element, you can chain the hide() method onto this line: 您的代码似乎没有试图隐藏任何东西,并且我不知道您是否故意将其遗漏,但是要隐藏元素,可以将hide()方法链接到此行:

    $(this).parents('.pulled_in_html').hide();

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

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