2012-03-17 81 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

相關問題