dqb14659 2012-11-20 18:27
浏览 33
已采纳

如何使jQuery load()适用于所有数组元素,而不仅仅是最后一个元素

I have a web page that show a list of data feeds by name (the dataFeed variable below) and I am trying to simulate a pop-up window that contains more information about the data feed when the user clicks on the icon (dataFeed + 'dataLink') next to the data feed name. I have a hidden div (diagWindow) that contains a div (diagWindowContent) that is populated with this information about the feed. The getDiagData.php page returns this information that I want to display, using the dataFeed parameter passed to it.

At first I tried this...

for (i = 0; i < dataFeeds.length; i++) {
    dataFeed= dataFeeds[i];

    $('#' + dataFeed+ 'diagLink').click(function() {
        $('#diagWindow').toggle();
        $('#diagWindowContent').load('getDiagData.php?dataFeed=' + dataFeed);
    });
}

but that only displayed the page returned for the last dataFeed in the dataFeeds array.

Then I tried using a a callback method on the toggle(), like this...

for (i = 0; i < dataFeeds.length; i++) {
    dataFeed= dataFeeds[i];

    $('#' + dataFeed+ 'diagLink').click(function() {
        $('#diagWindow').toggle('fast', function(dataFeed) {
            return function() {
                $('#diagWindowContent').load('getDiagData.php?dataFeed=' + dataFeed);
            }(dataFeed)
        );            
    });
}

but that appeared to have the same result, displaying the information for the last data feed in the dataFeeds array.

I am seeking help on figuring out how to make the appropriate data feed information load from the getDiagData.php page when clicking on the icon next to the data feed name.

Thank you.

  • 写回答

2条回答 默认 最新

  • dongxie8856 2012-11-20 18:31
    关注

    Try this.. You are not executing it in the correct context ..

    for (i = 0; i < dataFeeds.length; i++) {
        (function(num){
            var dataFeed = num;
    
            $('#' + dataFeed + 'diagLink').click(function() {
                $('#diagWindow').toggle();
                $('#diagWindowContent').load('getDiagData.php?dataFeed=' + dataFeed);
             });
        })(dataFeeds[i])
    }​
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?