我正在使用jquery raty plugin,我使用click event来发送评级数据,然后从数据库返回新的评级,但我无法弄清楚如何更新返回的评级,我的代码:
$('.starrating').raty({
number:10,
score: function() {
return $(this).attr('data-rating');
},
click: function(score, evt) {
var contentid = $(this).attr('id');
$.post('/main/rating.php',{score:score, contentid:contentid },
function(data){
//alert(data+' Score = '+score);
$(this).data('rating',2);
});
}
});
我试过以下但没有成功;
$(this).data('rating',2);
感谢您的帮助。
答案 0 :(得分:2)
根据raty docs
尝试$(this).raty({ score: 2 });
P.S。如果您另外需要设置数据属性,可以尝试:$(this).raty({ score: 2 }).attr('data-rating', 2);
P.P.S。正确处理多个元素的click
事件更新
$('.starrating').raty({
number:10,
score: function() {
return $(this).attr('data-rating');
},
click: function(score, evt) {
var target = $(this),
contentid = target.attr('id');
$.post('/main/rating.php',{score:score, contentid:contentid },
function(data){
target
.raty({
score: data.score
})
.attr('data-rating', data.score);
});
}
});
答案 1 :(得分:0)
根据您对重置参数的评论,我在下面添加了一些想法。
//SET RATY DEFAULTS
$.fn.raty.defaults.path = 'img';
$.fn.raty.defaults.cancel = true;
//etc
在您的成功功能中,使用新数据重置评级。只会覆盖指定的选项。
$(this).raty('set', { option: value });
例如,更新分数
$(this).raty('set', { score: 2 });
如果您在使用this
时遇到问题,请尝试下面的代码(基于此答案Passing variables through to AJAX)
$.fn.raty.defaults.click= function(score, evt)
{
var target = $(this),
contentid = target.attr('id');
postvalue(score,target, contentid);
}
function postvalue (score,target, contentid)
{
$.post('/main/rating.php',{score:score, contentid:contentid },
function(data)
{
target.raty({score: data.score}).attr('data-rating', data.score);
});
}
注意:此代码均未经过测试。
答案 2 :(得分:0)
对此有些麻烦,这对我有用:
{{1}}