【问题标题】:How can I get the value from text field in a Fancybox iframe on closing如何在关闭时从 Fancybox iframe 中的文本字段中获取值
【发布时间】:2014-10-27 15:29:42
【问题描述】:

我在fancybox(2) iframe 中有一个测验,完成后我在Fancybox 页面上的隐藏文本框中保存了一个分数:

<form>
      <input type="text" id="theresult" hidden="true"/>
</form> 

现在当用户关闭 Fancybox 时,我需要捕获这个值供父页面使用,我已经尝试过:

$('.fancybox1').fancybox({
        width: 800,
        height: 500,    
        beforeClose: function () {
                 var testResult = $("#fancybox-frame").contents().find('input#theresult');
                 $('#txtTestValue').attr('value', testResult );
        }
});

但这会返回“[object Object]”...

我做错了什么?!

【问题讨论】:

    标签: javascript jquery fancybox fancybox-2


    【解决方案1】:

    由于您使用的是fancybox v2.x,您可能需要定位正确的选择器,所以试试这个

    beforeClose : function () {
        var testResult = $(".fancybox-iframe").contents().find("#theresult").val();
        $('#txtTestValue').attr('value', testResult);
    }
    

    注意变化:

    $("#fancybox-frame")
    

    ...应该是

    $(".fancybox-iframe")
    

    ...我还添加了.val() 方法。

    【讨论】:

    • 如果您想了解更多如何在父页面和 iframed 页面之间来回传递变量,您可能需要查看stackoverflow.com/q/26426934/1055987
    • 你在 JFK 的头部击中了它 :-) 工作得很好。谢谢!
    【解决方案2】:

    您正在存储输入字段本身。您应该存储输入的值:

    var testResult = $("#fancybox-frame").contents().find('input#theresult').val();
    

    【讨论】:

    • 谢谢 - 刚刚试过,但现在它只返回“0”
    • 尝试使用.text() 而不是.val()
    • 您应该发布您当前设置的小提琴,以便我们可以更好地帮助您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2015-01-28
    相关资源
    最近更新 更多