javascript - 如何在 php 程序中的 jQuery/Ajax 函数中指向正确的表行

标签 javascript php jquery html ajax

我正在努力告诉我的 jQuery 函数(位于 php 程序中)来识别动态生成的表中的正确行,该表在表的每一行中显示一张照片。我想使用 Ajax“加载”方法将属于照片的标签加载到用户单击的照片表中特定行的单元格中的 div 中。该表中的行数是由一个我事先不知道其值的变量定义的。表中的每一行都有以下 div:

echo "
$Photo_Counter = 0; // set $Photo_Counter to zero before  
    searching through photos in a database
<table>
    ...foreach loop // this loop brings up all the photos found 
          in a search through a database
    $Photo_Counter = $Photo_Counter + 1;
    ...
    <tr>
        <td>$Photo_Counter</td> // this shows the number of the row
        <td>
            <div class='see_raw_tags'> 
                <a href='flickr_photos_getInfo.php?Photo_ID=$photo_id'>see the tags</a>
            </div>                            
            <div class='raw_tags'> //this is the place that should receive the photo's tags
            </div>";
        </td>
        <td>$Photo</td> //this shows a photo
    </tr>
</table>

每个表格行中的 div class='see_raw_tags' 都包含一个唯一的超链接,用户可以单击该超链接来查看该行中照片的标签。这将触发 jQuery 函数使用 Ajax 加载方法将标签加载到空的“raw_tags”div 区域中。

这是 jQuery/Ajax 编码:

<script>
$(document).ready(function()
{
    $('.see_raw_tags a').click(function(e)
    {        
        var url = $(this).attr('href');
        $('.raw_tags:nth_child('+photo_counter')').  
            html('loading...').load(url);
        var photo_counter = <?php echo $Photo_Counter; ?>;
        e.preventDefault();
    });
});
</script>

在生成表期间,我有一个在 php 中运行的行计数器(称为:$Photo_Counter)。现在,我想使用 php 变量 $Photo_Counter 将照片标签仅插入到用户单击的行中,方法是首先将其转换为名为 photo_counter 的 JavaScript 变量。但它不起作用。我只是看到正确的标 checkout 现在新的浏览器屏幕中,而不是出现在带有照片的表格中的相应单元格和行中。

问题:我确信问题出在 jQuery 编码中。但我在这里缺少什么?我做错了什么?

最佳答案

是新窗口还是你当前窗口的html因为html功能被标签替换了?

尝试一下。 $(this).htm... 而不是 html() 并从那里开始。

如果我是你,我会将行的 id 添加到包含 url 的对象中,因此在渲染表格时,你会添加一个 href... data-rownum="$photocounter"并且在你的 js 中你会这样做$(this).parent().load(...)

关于javascript - 如何在 php 程序中的 jQuery/Ajax 函数中指向正确的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340299/

相关文章:

jquery - slidedown 和 z-index 问题?

javascript - 互联网浏览器 11 JavaScript : Object are static

javascript - JavaScript 中的 JSON 格式问题

javascript - Jasmine 依赖注入(inject)

php - CakePHP 到 CodeIgniter 的转换

php - 连接 2 个 echo 并用逗号分隔 - 内爆、数组

javascript - JQuery 延迟数组,实现不工作

javascript - jQuery读取TD的所有表数据

javascript - 无法在 React.js 上从 svg 加载图像

javascript - 如何在存储在数组中的 html vue js 中提供编辑功能?