josh
josh

Reputation: 33

jquery - rating plugin

I am trying to display current rate and allowing re-rating on the same inputs, but it doesn't work. I could display current rate but disable re-rating or vice versa. I am using that plugin: http://www.fyneworks.com/jquery/star-rating/#tab-API . Here is what I got:

Here I got version with properly display current rate:

$('.voting-star').rating().rating('select', '3');//

        $('.voting-star').rating({
            callback: function(value, link){
                alert(value);
                $.ajax({
                    url: '/false/address/xxx?rate=' + value + '&photo_id=123425',
                });
            }
        });

And here is version with allowed re-rating (but when site is ready I can see that 'alerts' shows many times - and the same number AJAX request is started:

    $('.voting-star').rating({
        callback: function(value, link){
            alert(value);
            $.ajax({
                url: '/false/address/xxx?rate=' + value + '&photo_id=234234',
            });
        }
    });

    $('.voting-star').rating().rating('select', '%{$current_img->getLoggedUserRate()}%');

Upvotes: 2

Views: 1085

Answers (2)

Andrew Whitaker
Andrew Whitaker

Reputation: 126082

The problem is that the callback function is being called multiple times when you call select through the API. You could do something like this instead:

/* Check the appropriate rating (replace with server side code if necessary): */
$(".voting-star[value='3']").attr("checked", true);

/* Initialize the rating plugin: */
$('.voting-star').rating({
    callback: function(value, link) {
        alert(value);
    }
});

Here's a working example (without AJAX): http://jsfiddle.net/ErmRS/1/

Upvotes: 1

GG.
GG.

Reputation: 21894

I advise you Raty

$('#star').raty({
    half: true,
    start: /* your current average */
}).click(function() {
    $.ajax /* etc */
});

Upvotes: 1

Related Questions