0

我是 AJAX 新手,我一直在努力编写 ajax 代码,但我很喜欢它。我的问题是我有一个循环数据,每个数据都有一个投票链接功能。我能够执行 ajax 功能,但是一旦我单击每个数据,它只会给我一个值,这是我从数据库中获得的第一个值。这是我的代码:

<span id="question">Vote for your Recipe!</span>

<?php $getData = $recipes->getBreakfast(); foreach($getData as $data) { ?>
<div class="item"><?php echo $data['recipe_id'] ?> </div>

<div class="score"><?php echo $data['vote']; ?></div>
<div class="a"><a href="">Vote</a><?php echo $data['recipe_title']; ?></div>

我的阿贾克斯:

   $(document).ready(function() {
    var self = $(this);
    //var id = $(".item").html();
    var score = $(".score").html();

    $.ajaxSetup({
        url: 'insert_vote.php',
        type: 'POST',
        cache: 'false',


  });

    $(".a").click(function() {

    alert(score);

       //self.find('.score').html(++score);

       return false;

    });
});

为你的食谱投票!

6 投票鸡

0 票

4 投票热狗

即使我点击另一个食谱,我也只能得到 6 分值

4

1 回答 1

1

remove

  var score = $(".score").html();

and use

$(".a").each(function() {
 $(this).click(function() {
   var score = $(this).prev().html();
   alert(score);
   return false;
 });
});
于 2013-11-01T08:04:28.667 回答