简体   繁体   English

使用Ajax数据更改Fancybox iframe的内容

[英]Change contents of fancybox iframe with ajax data

I have a template site which contents I want to change with data from an ajax call. 我有一个模板站点,我想用ajax调用中的数据更改其内容。

I have a button which opens an fancybox 3 iframe. 我有一个打开fancybox 3 iframe的按钮。 The content of this page is what I want to change. 该页面的内容是我要更改的内容。

Button to open fancybox: 打开fancybox的按钮:

'<i class="fa fa-info-circle fa-2x inv-details" invoiceId="' + data[index].invoiceid + '"' +
'aria-hidden="true" data-fancybox data-type="iframe" data-type="iframe" href="javascript:;" data-src="/invoice_details/"</i>'

click event to trigger ajax call: click事件触发ajax调用:

$('#my_in').on('click', 'i.inv_details', function () {
    var inv_id = $(this).attr("invoiceId");
    $.ajax({
        url: '/wp-admin/admin-ajax.php',
        type: 'post',
        dataType: 'json',
        data: {
            action: 'invoice_details_ajax',
            inv_id: inv_id
        },
        done: function (data) {

            //HERE I WANT TO FIND THE CONTENTS OF THE 
            //OPEN IFRAME AND CHANGE THEM, LIKE SUCH:
            // $('.fancybox-iframe').contents().find("#test #bankgiro").text('test');
            // This above works in console, if I load jquery first, it does not work here.


        }
    });
});

The jquery selector for the iframe is correct, since i can edit the page via the chrome console. iframe的jquery选择器是正确的,因为我可以通过chrome控制台编辑页面。 So I think the problem is that it tries to change the page before it has loaded? 所以我认为问题在于它试图在加载页面之前更改页面? Is there any solution for this? 有什么解决办法吗?

All answers I've read assums you have an empty fancybox iframe page and simply append data to it. 我读过的所有答案都假设您有一个空的fancybox iframe页面,只需将数据附加到该页面即可。 But in my case I already have an template site which contents i want to change. 但就我而言,我已经有一个模板站点,我想更改其内容。 Links I have read: 我已阅读的链接:

How to show ajax response in fancybox iframe Loading dynamic AJAX content into Fancybox 如何在fancybox iframe中显示ajax响应将 动态AJAX内容加载到Fancybox中

It is not clear from your description - you want to open iframed page inside fancybox and you want to make ajax request that changes contents of that page - at the same time? 从您的描述尚不清楚-您想同时打开fancybox中的iframed页面,并且要发出aajax请求来更改该页面的内容-同时? Would it not be more sensible to just load final page? 仅加载最终页面是否更明智?

I think you just have to pass invoice id using url, like /invoice_details/?invoiceId=YOUR_ID and avoid messing with two requests. 我认为您只需要使用url传递发票ID,例如/invoice_details/?invoiceId=YOUR_ID并避免弄乱两个请求。

If you've already tried document.ready and window.onload etc which I assume you have seems as you're using jQuery: 如果您已经尝试过document.ready和window.onload等,我认为您似乎在使用jQuery:

The way I tend to get around things like this is I but a setInterval() that fires every 0.5 seconds or whatever, checks if the needed info is there and if it is, does what it needs to and then turns itself off. 我倾向于解决这样的问题的方法是我,但是每隔0.5秒触发一次setInterval(),检查是否存在所需的信息,是否存在,执行所需的操作,然后自行关闭。 Could work for you for needing to wait for page load? 是否可以为需要等待页面加载而工作?

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

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