doujia4759 2019-01-24 16:38
浏览 82
已采纳

在foreach循环中右键单击菜单

I have a foreach loop where I am parsing a json return. Parsing works and I am able to echo what I need. Each return is displayed in a table cell.

I want to have a right click menu in each table cell with a hyperlink where the return will also appear. I have tried a few right click examples and no matter where I put the right click menu, I always see the last item in the loop appear in the hyper link.

My hyperlink is

      <a href = "test.com?cmd=<?php echo $json_item;?>ITEM_1</a>

While the table cells all display $json_item properly, the right menu item ITEM_1 is displaying the last value of $json_item

  • 写回答

1条回答 默认 最新

  • doujiaohuo1096 2019-01-24 16:41
    关注

    If your code looks like this:

    var txt = ["a","b","c"];
    
    for (var i = 0; i < 3; ++i ) { 
       var msg = txt[i];
        setTimeout(function() { alert(msg); }, i*1000);        
    }​
    
    // Alerts 'c', 'c', 'c'
    

    Your problem is hoisting related:

    ref: http://ignaciothayer.com/post/a-dangerous-example-of-javascript-hoisting/

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

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题