将值从fancybox iframe传递给parent [英] pass value/variable from fancybox iframe to parent

查看:170
本文介绍了将值从fancybox iframe传递给parent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将fancybox iframe中的变量/值传递给父窗口但没有成功。

I'am trying to pass a variable/ value from the fancybox iframe to the parent window without success.

Fancybox是从链接启动的

Fancybox is launched from a link with

   class="fancybox fancybox.iframe"

我在fancybox.iframe中的代码是:

My code in the fancybox.iframe is:

$(document).ready(function(){
   $('.insert_single').click(function(){
    var test =  $('.members_body').find('{row.U_USERNAME}');
    setTimeout(function(){ parent.$.fancybox.close();},300);return true;

   });
});

{row.U_USERNAME}是iframe中要查找的用户名。

Where '{row.U_USERNAME}' is the username to find in the iframe.

然后,在父母中有以下代码:

Then, in the parent there's the following code:

 $(document).ready(function(){
 $('.fancybox').fancybox(

  {
openEffect:'fade',
openSpeed:500,


    afterClose: function(){
    alert($(".fancybox-iframe").contents().find(test)); 
    $('#form input[name=username]').val()(test);return false;
    }
}                   

  );
 });

但是当fancybox关闭时,没有警告显示变量test,也没有变量在表单的输入字段中显示为值或文本。

But when the fancybox is closed, there's no alert showing up with the variable "test", nor the variable is showing up as a value or as a text in the input field of the form.

我已经阅读并尝试了在stackoverflow上找到的各种解决方案,但没有成功。

I've read and tried various solutions found here on stackoverflow without success.

提前感谢您的帮助

编辑

这是一个示例

推荐答案

当fancybox关闭时,iframe将从文档中删除。所以你必须使用 beforeClose 事件而不是 afterClose

When the fancybox is closed the iframe is removed from the document. So you must use beforeClose event instead of afterClose

$(document).ready(function() {
    $('a.fancybox').fancybox({
        openEffect:'fade',
        openSpeed:500,
        beforeClose: function() {
            // working
            var $iframe = $('.fancybox-iframe');
            alert($('input', $iframe.contents()).val());
        },
        afterClose: function() {
            // not working
            var $iframe = $('.fancybox-iframe');
            alert($('input', $iframe.contents()).val());
        }
    });
});

JSFiddle: http://jsfiddle.net/NXY7Y/1/

JSFiddle: http://jsfiddle.net/NXY7Y/1/

编辑:

我编辑了你的jsfiddle( http://jsfiddle.net/NXY7Y/9 / )。更新在此链接
http://jsfiddle.net/NXY7Y/13/

I edited your jsfiddle (http://jsfiddle.net/NXY7Y/9/). Update is in this link http://jsfiddle.net/NXY7Y/13/

主页javscript:

Main page javscript:

$(document).ready(function() {
    $('a.fancybox').fancybox({
        openEffect:'fade',
        openSpeed:500//,
        //beforeClose: function() {
        //    // working
        //    var $iframe = $('.fancybox-iframe');
        //    alert($('input', $iframe.contents()).val());
        //},
        //afterClose: function() {
        //    // not working
        //    var $iframe = $('.fancybox-iframe');
        //    alert($('input', $iframe.contents()).val());
        //}
    });
});

function setSelectedUser(userText) {
    $('#username').val(userText);
}

无需使用 afterClose beforeClose 事件。只需从链接点击事件上的iframe访问父函数 setSelectedUser ,如下所示:

No need to use afterClose or beforeClose events. Just access the parent function setSelectedUser from the iframe on link click event like this:

$(document).ready(function() {
    $('a.insert_single').click(function() {
        parent.setSelectedUser($(this).text());
        parent.$.fancybox.close();
    });
});

这篇关于将值从fancybox iframe传递给parent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆