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 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测