2011-01-19 84 views
2

我有這個投票系統的WordPress的安裝一切正常,如果我只有一個職位,如果我有更多的AJAX職位只爲第一種形式。

所以每一個崗位都有HTML表單:

<form method="post" id="vote_<?php the_ID(); ?>" class="vote" name="vote"> 
    <input type="hidden" id="vote_post_id" name="vote_post_id" value="<?php the_ID(); ?>" /> 
    <input type="hidden" id="vote_count" name="vote_count" value="1" /> 
    <span class="votes_count"><?php echo get_post_meta($post->ID, 'votes', true); ?></span> 
    <input type="submit" name="vote_up" class="vote_up" value="+1" /> 
</form> 

而這裏的jQuery的:

$(function() { 
    $('form.vote').submit(function() { 
     var url = 'http://bla-bla.com/lib/'; 
      post_id = $('input#vote_post_id').attr('value'); 
      count = $('input#vote_count').attr('value'); 
      form_id = $('form.vote').attr('id');  
      cast_vote = 'post_id=' + post_id + '&count=' + count; 
      original_count = $('span.votes_count').html(); 
      new_value = parseInt(original_count) + 1; 

     //alert (cast_vote);return false; 
     $.ajax({ 
      type: 'POST', 
      url: url + 'cast.php', 
      cache: false, 
      timeout: 10000, 
      data: cast_vote, 
      success: function(data){ 
       $('form.vote').find('span.votes_count').fadeIn(1500, function() { 
        $(this).html(new_value); 
       }); 
       $('form.vote').removeClass('vote').addClass('user_voted disabled'); 
      } 
     }); 
     return false; 
    }); 

}); 

所以我想這個工作,只能在已submited形式。我如何實現這一目標?

.each()會工作嗎?

回答

3

如果我正確地理解這個(如果不只是不理我大聲笑)...

您可以使用.live功能舉行提法,每個提交頁面上的按鈕...

$('.vote-up').live('click', function(){ 

     //do your ajax calls here... 

}); 

這將基本上爲每個投票的按鈕創建一個點擊事件...

+0

謝謝你,工作。 – 2011-01-19 15:38:41