duanboxue3422
duanboxue3422
2013-08-13 03:29

无法在javascript弹出窗口中检索文本框值

I want to pass a text box value in parent.php to my popup window(child_page.php). Following is my code. parent.php-

<html>
<head>
<script type="text/javascript">

var popupWindow=null;

function child_open()
{ 

popupWindow =window.open('child_page.php',"_blank","directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200");

}
function parent_disable() {
if(popupWindow && !popupWindow.closed)
popupWindow.focus();
}
</script>

 <input type="text" name="myTextField" id="myTextField" required="required"/>


</head>
<body onFocus="parent_disable();" onclick="parent_disable();">
    <a href="javascript:child_open()">Click me</a>
</body>    

</html>

child_page.php

<h1>child page</h1>


<script>
    window.onunload = refreshParent;
    function refreshParent() {
        window.opener.location.reload();
    }
    var x=parent.document.getElementById('myTextField').value

    <?php echo x; ?>
</script>

<?php 

//how do I get the textbox value here.

    ?>

I googled it and tried to do it. Since I know little about java script couldn't pull those answers together as a solution.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongzaotiao2863 dongzaotiao2863 8年前

    There are many errors in your html. You are writing input element before closing head tag. Move it inside body. And try something like this:

    <html>
    <head>
    <script type="text/javascript">
    
        var popupWindow=null;
    
        function child_open(val){ 
            popupWindow =window.open('child_page.php' + "?val=" + val,"_blank","directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200");
        }
    
        function parent_disable() {
            if(popupWindow && !popupWindow.closed)
                popupWindow.focus();
        }
    </script>
    
    </head>
    <body onFocus="parent_disable();" onclick="parent_disable();">
        <input type="text" name="myTextField" id="myTextField" required="required"/>
        <a href="#" onclick="var val = document.getElementById('myTextField').value; child_open(val);return false">Click me</a>
    </body>    
    
    </html>
    

    Now in your child_page.php you can get the value using $_GET['val'].

    //child_page.php
    
    <h1>child page</h1>
    <?php $x = $_GET['val']; ?>
    <script>
        window.onunload = refreshParent;
        function refreshParent() {
            window.opener.location.reload();
        }   
    </script>
    
    <?php echo $x; ?>
    
    点赞 评论 复制链接分享