0

在我的php中,我在while循环中有这样的代码......

$result = mysql_query("SELECT * FROM wallpost ORDER BY wallpostid DESC");
while($row = mysql_fetch_assoc($result)){
$rate = "<div class=\"example-".$id." \" data-postid=\"".$id."\"></div></br>Post id: <span class=\"example-rating-".$id."\">".$id."</span>";
}

jQuery是...

$(document).ready(function() {
$('[class^="example-"]').not('[class^="example-rating-"]').ratings(3).bind('ratingchanged', function (event, data) {
var child = $(this).find('[class^="example-rating-"]');
child.text(data.rating);

$.post('count.php', {
     rate: data.rating,
     wallpostid: jQuery(this).data("postid")
}, function (data) {
    alert(data);
});
});

对于值 AI 获得空值,但如果我替换

var a = $('.example-rating-50').html();  //let say the wallpostid is 50

它只能将值 50 传递给 count.php 如果现在假设我有 2 个 wallpostid,即 22 和 50(用 while 循环循环)如果我将 wallpostid 评为 22,那么我想从 php 传递 $id=22 的值到 jquery 和 $.post 到 count.php。如果我评价 wallpostid=50,请执行相同的操作。

4

3 回答 3

4

这是闭包变量的问题i

由于您i在回调内部使用,它将具有循环中的最后一个值,即 102,这就是它失败的原因

$(document).ready(function () {
    for (var i = 1; i < 101; i++) {
        (function(idx){
            $('.example-' + idx + '').ratings(3).bind('ratingchanged', function (event, data) {
                $('.example-rating-' + idx + '').text(data.rating);
                var a = $('.example-rating-' + idx + '').html();

                $.post('count.php', {
                    rate: data.rating,
                    wallpostid: a
                }, function (data) {
                    alert(data);
                });
            });
        })(i)
    }
});

更好的解决方案可能是

$rate = "<div class=\"examples example-".$id." \" data-idx=\"".$id"\"></div></br>Post id: <span class=\"example-rating-".$id."\">".$id."</span>";

然后

$(document).ready(function () {

    $('.examples').ratings(3).bind('ratingchanged', function (event, data) {
        var i = $(this).data('idx')
        var a = $('.example-rating-' + i + '').text(data.rating);

        $.post('count.php', {
            rate: data.rating,
            wallpostid: data.rating
        }, function (data) {
            alert(data);
        });
    });

});
于 2013-08-08T05:27:40.300 回答
0

我喜欢不动态构建元素选择器。

var $examples = $('[class^=example-]:not([class^=example-rating])');
var $ratings = $('[class^=example-rating]');

$examples.ratings(3).bind('ratingchanged', function (event, data) {
  var index = $examples.index(this);
  var $rating = $ratings.eq(index);
  $rating.text(data.rating);
  var a = $rating.attr('class').split('-')[2];

  $.post('count.php', {
    rate: data.rating,
    wallpostid: a
  }, function (data) {
    alert(data);
  });
});
于 2013-08-08T05:34:53.410 回答
0

您不需要在 javascript 中使用 for 循环,例如有可以处理此问题的选择器class^=

PHP

$rate = "<div class=\"example-".$id." \" data-postid=\"".$id."\"></div></br>Post id: <span class=\"example-rating-".$id."\">".$id."</span>";

JS

//Find elements that have class starting with 'example-' but Not example-rating- 
$('[class^="example-"]').not('[class^="example-rating-"]').ratings(3).bind('ratingchanged', function (event, data) {
    //Find Child element that has class starting with 'example-rating-'
    var child = $(this).find('[class^="example-rating-"]');
    child.text(data.rating);

    $.post('count.php', {
         rate: data.rating,
         wallpostid: jQuery(this).data("postid")
    }, function (data) {
        alert(data);
    });
});
于 2013-08-08T05:28:56.257 回答