doulai6469 2013-10-07 10:24
浏览 78
已采纳

javascript - 将值从父窗口传递到子窗口和函数内

I've a form that contains several images. Once the user clicks whichever image a popup window with thumbnails is come up. Once he clicks whichever thumbnail, the source image in the parent window needs to replaced according the user selection (swapping the original image). The following script works fine for one image only since logo1 and placeHolder1 variables are hard coded. How can I send the image id from the parent window to the popup window, so the following will be dynamic?

editPage.editForm.logo1.value = oName

editPage. placeHolder1.src = " images/user/" + oName;

BTW, the images name’s and id’s in the main window subject to be changed according the form template.

template.php

<script type="text/javascript">
function logoWin() {
window.open('logos.php','logos','height=500,width=700,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes');
}
</script>

<form action="" id="editForm" name="editForm" enctype="multipart/form-data">
<img src=”images/user/logo1.jpg” name="placeHolder1" id="placeHolder1" onclick='logoWin()'>
<img src=”images/user/logo2.jpg” name="placeHolder2" id="placeHolder2" onclick='logoWin()'>
<img src=”images/user/logo3.jpg” name="placeHolder3" id="placeHolder3" onclick='logoWin()'>

<input type="hidden" name="logo1" value="<?php echo $main_logo1; ?>" />
<input type="hidden" name="logo2" value="<?php echo $main_logo2; ?>" />
<input type="hidden" name="logo3" value="<?php echo $main_logo3; ?>" />

<input type="submit" value="Save" id="save">
</form>

logos.php

<script language="JavaScript">
    function getFile(oImg){
        editPage = eval(window.opener.document)
        oSrc = oImg.src;
        lastSlash = oSrc.lastIndexOf('/');
        oName = oSrc.substr(lastSlash+1);
        editPage.editForm.logo1.value = oName
        editPage. placeHolder1.src = " images/user/" + oName;
        this.close()
    }
    </script>   

[php loop]
<img src='images/user/".$row->img_file_name."' onclick='getFile(this)'>
<img src='images/user/".$row->img_file_name."' onclick='getFile(this)'>
<img src='images/user/".$row->img_file_name."' onclick='getFile(this)'>
  • 写回答

1条回答 默认 最新

  • doulu4976 2013-10-07 11:10
    关注

    Why you are using

    eval(window.opener.document);
    

    You can use

    var editPage = window.opener.document;
    

    You can pass the id with url of the page as given below

    function logoWin(id) {
        window.open('logos.php?id='+id,'logos','height=500,...');
    }
    

    Pass the id as function paarameter, like (logoWin(1), logoWin(2)) as given below

    <img src=”images/user/logo1.jpg” name="placeHolder1" id="placeHolder1" onclick='logoWin(1)'>
    <img src=”images/user/logo2.jpg” name="placeHolder2" id="placeHolder2" onclick='logoWin(2)'>
    

    In your logos.php file, get it id using

    $id = $_GET['id']; // it could be 1 or 2 or 3 and so on
    

    Then, you can use it as JavaScript variable like

    function getFile(oImg){
        var id = <?php echo $id; ?>;
        editPage = eval(window.opener.document);
        oSrc = oImg.src;
        lastSlash = oSrc.lastIndexOf('/');
        oName = oSrc.substr(lastSlash+1);
        var logo = 'logo'+id, placeHolder = 'placeHolder'+id;
        editPage.editForm[logo].value = oName;
        editPage[placeHolder].src = " images/user/" + oName;
        this.close();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?