2012-03-17 47 views
0

我正在研究一个WordPress插件“为我的评分”。此插件适用于单个帖子,但在博客页面上,当插件无法从哪个帖子ID完成点击时看到多个帖子时。它总是需要页面的firt post值。如何在jQuery脚本中获得动态帖子ID或值

我已经添加了ID和ID和类名来解决这个问题(例如:post_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> 

jQuery的代码(投票支持,否决是很相同):

jQuery(document).ready(function($) { 


     $(".vote_up").click(function(e) { 
     e.preventDefault(); 

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

      $(".vote_succ1").html(data); 
      $.post('/wp-content/plugins/rate-my-whatever/rate.php', {action: "getPercentage", vote_type: "1", post_id: $("#post_id1").val()}, function(data2) { 
       $(".up_perc1").html(data2); 
      }); 
      $.post('/wp-content/plugins/rate-my-whatever/rate.php', {action: "getPercentage", vote_type: "2", post_id: $("#post_id1").val()}, function(data3) { 
       $(".down_perc1").html(data3); 
      }); 
     }); 
    }); 

我把staticly “1” 后一些ID和类元素来检查我的问题将如何解决,它适用于Post 1的哪个ID和值是“1”,现在我需要替换#post_id,.vote_succ,.up_perc末尾的“1” ,.down_perc通过动态代码使其与PHP代码生成的动态元素一起工作。

感谢您的帮助。

回答

2

您需要更改代码,以便它在单击的单元上运行,而不是在整个页面中该类的所有对象上运行。要做到这一点,最简单的办法是把各单位在一个容器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> 

而且,然后更改代码,以查找在同一表决单元所涉及的对象通过使用$(this).closest('.voteUnit').find()在点击的投票单元中查找对象,而不是查看整个网页并在所有投票单元中查找对象,从而点击该对象。 .closest('.voteUnit')从点击的项目中查找祖先链,直到找到具有class=voteUnit的父项。代码然后可以使用它作为子树来在投票单元中搜索其他对象。

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); 
     }); 
    }); 
}); 
+0

非常感谢jfriend00,非常漂亮的工作,它的伟大工程;) – 2012-03-19 15:09:04

+0

@FannyAuray - 因为它似乎你是新来的计算器,你明白,当你问一个问题,你会得到一个或更体面答案时,您最终应通过单击答案左侧的复选标记(仅在上/下箭头下方)将其中一个答案标记为“最佳答案”?这会为你和获得答案的人赢得一些声望点。 – jfriend00 2012-03-19 15:14:13

+0

感谢您的信息,愉快地完成! – 2012-03-19 16:06:16