简体   繁体   English

如何使用JavaScript或JQuery从不可见的iframe获取隐藏字段值

[英]How to get hidden field value from invisible iframe using javascript or JQuery

Ok it's 01:16AM here and i can't solve this simple problem. 好的,现在是01:16 AM,我无法解决这个简单的问题。 I use the invisible iframe to upload photo and send some text into DB. 我使用不可见的iframe上传照片并将一些文本发送到数据库中。 The iframe point to upload.php and return me hidden textfield with insert_id (the id of the last row in DB) - i need this information for other function. iframe指向upload.php,并向我返回带有insert_id(数据库中最后一行的ID)的隐藏文本字段-其他功能我需要此信息。

Here is how my iframe: 这是我的iframe:

 <iframe name="image_upload_frame" id="image_upload_frame" height="1" width="1" style="visibility: hidden">

It is empty at the beginning, but after image upload, it has this html: 开头是空的,但是在图片上传后,它具有以下html:

 echo '<img src="'.$sImage.'" />';
     echo '<input type="hidden" id="session_id" value="'.$wpdb->insert_id.'" />';

Here is my javascript to get session)id value: 这是我的JavaScript以获取session)id值:

var row_id = $("#image_upload_frame").contents().find("#session_id");
  alert(row_id.val());

It doesn't work. 没用 Debugger says nothing - it just stop, no error, no notification. 调试器什么也没说-它只是停止,没有错误,没有通知。

Anyone can help? 有人可以帮忙吗? I'd like to hear for other options how to pass data from iframe to the main window. 我想听听其他选项如何将数据从iframe传递到主窗口。

Thanks, 谢谢,

if i'm not assuming wrong, your html is like this: 如果我没有假设错误,则您的html如下所示:

<html>
<body>
    <iframe id="hidden_iframe" name="hidden_iframe" style="display: none"></iframe>
    <form method="post" target="hidden_iframe" action="./test1.php" encrypt="multipart/form-data">
        <input type="file" name="file_upload" />
        <input type="submit" value="submit" />
    </form>
</body>

my idea is in your server side , you write a javascript code to call your client side's javascript(actural is iframe call main window).so your client code should like this: 我的想法是在您的服务器端,您编写一个javascript代码来调用客户端的javascript(实际上是iframe调用主窗口)。因此,您的客户端代码应如下所示:

<html>
<body>
    <iframe id="hidden_iframe" name="hidden_iframe" style="display: none"></iframe>
    <form method="post" target="hidden_iframe" action="./test1.php" encrypt="multipart/form-data">
        <input type="file" name="file_upload" />
        <input type="submit" value="submit" />
    </form>
    <script>
    function callFromServerSide(params){
        alert(params);
    }        
    </script>
</body>

and you server side should like this(in my case is test1.php): 并且服务器端应该这样(在我的情况下是test1.php):

<?php
$last_insert_id=100;
echo "
<script>
window.top.callFromServerSide('".$last_insert_id."');                    
</script>";
?>

have fun.. 玩得开心..

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

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