如何在jquery脚本中获取动态post ID或值

如何在jquery脚本中获取动态post ID或值,jquery,wordpress,plugins,vote,Jquery,Wordpress,Plugins,Vote,我正在开发一个wordpress插件“给我的任何东西打分”。该插件在单个帖子上运行良好,但在博客页面上,当有多篇帖子时,该插件无法看到点击是从哪个帖子id完成的。它始终采用页面的第一个post值 我已经将post id添加到id和类名中来解决这个问题(例如:post\u id$post->id),但是现在我用这种方式阻止了jquery文件 投票后项目的php代码: <input type=\"hidden\" id=\"post_id$post->ID\" value=\"" .

我正在开发一个wordpress插件“给我的任何东西打分”。该插件在单个帖子上运行良好,但在博客页面上,当有多篇帖子时,该插件无法看到点击是从哪个帖子id完成的。它始终采用页面的第一个post值

我已经将post id添加到id和类名中来解决这个问题(例如:post\u id$post->id),但是现在我用这种方式阻止了jquery文件

投票后项目的php代码:

 <input type=\"hidden\" id=\"post_id$post->ID\" value=\"" . $post->ID . "\" />
    <div class=\"vote\">
<table>
    <tr><td>
    <a class=\"vote_up\" href=\"#\"></a></td>
<td>
    <a class=\"vote_down\" href=\"#\"></a></td></tr>
    <tr>
<td class=\"up_perc$post->ID\">" . get_percentage(1, $post->ID ) ."%</td>
<td class=\"down_perc$post->ID\">" . get_percentage(2, $post->ID) . "% </td>
</tr>
    </table></div>

 <div class=\"vote_succ$post->ID\"></div>
我在一些id和类元素之后静态地放上“1”来检查我的问题是如何解决的,它可以与id和值为“1”的Post 1一起工作,现在我需要用动态代码替换#Post#id、.vote#succ、.up#perc、.down#perc末尾的“1”,使其与php代码生成的动态元素一起工作


谢谢你的帮助

您需要更改代码,使其在单击的单元上运行,而不是在整个页面中该类的所有对象上运行。最简单的方法是将每个单元放在一个容器div中,如下所示,只使用类名,不使用ID值:

<div class=\"voteUnit\">
    <input type=\"hidden\" class=\"post_id\" value=\"" . $post->ID . "\" />
    <div class=\"vote\">
    <table>
        <tr>
            <td><a class=\"vote_up\" href=\"#\"></a></td>
            <td><a class=\"vote_down\" href=\"#\"></a></td>
        </tr>
        <tr>
            <td class=\"up_perc\">" . get_percentage(1, $post->ID ) ."%</td>
            <td class=\"down_perc\">" . get_percentage(2, $post->ID) . "% </td>
        </tr>
    </table>
    </div>

    <div class=\"vote_succ\"></div>
</div>

@FannyAuray-由于您似乎是stackoverflow的新手,您是否了解当您提出一个问题并得到一个或多个体面的答案时,您最终应该通过单击答案左侧的复选标记(上/下箭头下方)将其中一个答案标记为“最佳答案”?这将为您和回答者赢得一些声誉积分。
<div class=\"voteUnit\">
    <input type=\"hidden\" class=\"post_id\" value=\"" . $post->ID . "\" />
    <div class=\"vote\">
    <table>
        <tr>
            <td><a class=\"vote_up\" href=\"#\"></a></td>
            <td><a class=\"vote_down\" href=\"#\"></a></td>
        </tr>
        <tr>
            <td class=\"up_perc\">" . get_percentage(1, $post->ID ) ."%</td>
            <td class=\"down_perc\">" . get_percentage(2, $post->ID) . "% </td>
        </tr>
    </table>
    </div>

    <div class=\"vote_succ\"></div>
</div>
jQuery(document).ready(function($) { 
    $(".vote_up").click(function(e) { 
        var unit$ = $(this).closest('.voteUnit');
        e.preventDefault();

        $.post('/wp-content/plugins/rate-my-whatever/rate.php', {vote_type: "1", post_id: unit$.find(".post_id").val()}, function(data) {

        unit$.find(".vote_succ").html(data);
        $.post('/wp-content/plugins/rate-my-whatever/rate.php', {action: "getPercentage",  vote_type: "1", post_id: unit$.find(".post_id").val()}, function(data2) {
            unit$.find(".up_perc").html(data2);
        });
        $.post('/wp-content/plugins/rate-my-whatever/rate.php', {action: "getPercentage", vote_type: "2", post_id: unit$.find(".post_id").val()}, function(data3) {
            unit$.find(".down_perc").html(data3);
        });
    });
});