在iframe中获取父值?

时间:2013-08-05 06:23:58

标签: javascript jquery html html5

我有两个html文件。在PassValueIFrame.html我有一个引用inputForm.html的iframe。此外,我在PassValueIFrame.html中有一个隐藏字段,我正在尝试在inputForm.html中检索其值,但我没有得到它的值,它会警告为“undefined”。我在这里做错了吗?请帮帮我。

PassValueIFrame.html

<html>
  <head>
  <title>IFrame Example</title>
  </head>
<body>
<input type="hidden" class="Language" value="English">
<iframe name="iframe" id="iframe_id" src="inputForm.html" height="150" >
</iframe>
</body>
</html>

inputForm.html

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script language="javascript">
  $(document).ready(function() {
    alert($(this).parent().find('.Language').html());
  });

  </script>
   <title>IFrame Child Example</title>
</head>
<body>
<h1> Iframeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee </h1>
</body>

谢谢!

3 个答案:

答案 0 :(得分:7)

<input id="myInput" type="hidden" class="Language" value="English">

$("#myInput", window.parent.document).val();

答案 1 :(得分:3)

试试这个:

alert(parent.document.getElementsByClassName("Language")[0].value);

或将id(例如languageId)添加到隐藏元素并尝试

alert(parent.document.getElementById("languageId").value);

答案 2 :(得分:0)

您可以做的另一件事是在父窗口中定义一个函数并从iframe中调用它:

inputForm.html

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script language="javascript">
        $(document).ready(function() {
            window.parent.parent_function("Papa!");
        });
    </script>
    <title>IFrame Child Example</title>
</head>
<body>
    <h1> Iframeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee </h1>
</body>
</html>

PassValueIFrame.html

<html>
    <head>
    <title>IFrame Example</title>
    <script>
        function parent_function(str) {
            window.alert(str);
        }
    </script>
</head>
<body>
    <input type="hidden" class="Language" value="English">
    <iframe name="iframe" id="iframe_id" src="inputForm.html" height="150" >
    </iframe>
</body>
</html>
相关问题