dongluo3962 2013-05-31 11:41
浏览 22
已采纳

too long

I have a project in Zend Framework, that has on a page a menu with all products ordered by categories. I want to make a pop-up with name and description of a product. This should be shown on every click on a product. I use this code for my pop-up:

<script type="text/javascript">
 function show_popup(id) {
if (document.getElementById){
    obj = document.getElementById(id);
    if (obj.style.display == "none") {
        obj.style.display = "";
    }
}
}
function hide_popup(id){
if (document.getElementById){
    obj = document.getElementById(id);
    if (obj.style.display == ""){
        obj.style.display = "none";
    }
 }
}
</script>

<div id="my_popup" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:400px;height:400px;left:10px;top:300px">
                                                <div align="right">
                                                        <a href="javascript:hide_popup('my_popup')">Close</a>
                                                </div>
                                                        <h3>Product Name</h3>
                                                        <p>Product description</p>
                                        </div>

The problem is that it works with static text. My products are taken from DB like this:

<?php foreach ($products as $product):?>
 <a href="javascript:show_popup('my_popup')"> <?php echo $product['front_name'];?></a>
<?php endforeach;?>

So I have a link for the pop-up on every title, but I want to print name and description of the product is clicked in that pop-up and I don't know how to verify which is the product that is clicked. I was thinking to send some parameters, but I am not sure how to do that. If you have some solutions for my problem, please do tell! Thank you

I've tried to put my **<div id="my_popup>**  in foreach, but it doesn't work because my pop-up windows does not appear anymore.
  • 写回答

1条回答 默认 最新

  • dongzouh51192 2013-06-06 10:29
    关注

    I encountered the same problem with Jquery, my solution was calling the JS function by providing it the id of the product so that it is unique,and concatenation with a alphanumeric (cause js dont accept only numeric in DIV): for example (inside your loop):

    <?php $id_for_div="unique_id_".$product['id'];
          $id_product=$product['id'];?>
    

    then pass that new id to the JS function showPopup('<?=$id_product?>'); and the id of the DIV must be :<div id='<?id_for_div ?>' ..>..</div>

    then in your JS function you can get the new parameter and do staff acording to it (in Jquery i do showPopup(id){ $('#unique_id_'+id).....} i hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?