我的页面上运行了以下jQuery脚本:
<script type="text/javascript" charset="utf-8" src="{{ STATIC_URL }}js/raty/js/jquery.raty.min.js"></script>
<script type ="text/javascript">
jQuery(document).ready(function($) {
$('.yourrating').each(function(index){
$(this).raty({
readOnly: false,
path: "{{ STATIC_URL }}js/raty/img/",
score: $(this).children("span:first").text(),
click: function(score, evt) {
var vote_url = "/spice/rate/" + this.attr('id').substring(2) + "/" + score + "/";
$.ajax({
url: vote_url,
success: function(){
alert('vote successful');
}
});
}
});
$('.thingrating').raty({
readOnly: true,
start: 2,
path: "{{ STATIC_URL }}js/raty/img/",
score: $(this).children("span:first").text(),
});
});
});
</script>
现在,当用户点击它时,它无法识别操作并显示弹出窗口,这告诉我变量没有正确显示。为了确认这一点,我将var vote_url
更改为直接路径"spice/rate/1/2"
,这确实正常运行。
这导致两个问题:
以下是模板中相关的代码片段:
<div class="yourrating" id="t_{{ item.id }}"><span style="display:none;">{{score}}</span></div>
答案 0 :(得分:1)
为什么我不为你清理一下,并给你一个接受的答案!
<script type ="text/javascript">
(function($) {
$(function() {
$('.yourrating').each(function(index,elem){
$(elem).raty({
readOnly: false,
path: "{{ STATIC_URL }}js/raty/img/",
score: $(elem).children("span:first").text(),
click: function(score, evt) {
var vote_url = "/spice/rate/" + this.id.substring(2) + "/" + score + "/";
$.ajax({
url: vote_url,
success: function(){
alert('vote successful');
}
});
}
});
$('.thingrating').raty({
readOnly: true,
start: 2,
path: "{{ STATIC_URL }}js/raty/img/",
score: $(elem).children("span:first").text(),
});
});
});
})(jQuery);
</script>