1

我的页面上运行了以下 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",这确实工作正常。

这导致两个问题:

  1. 我如何调试它以查看正在使用哪个 URL(即,被替换的变量?)
  2. 我该如何纠正这个问题?

以下是模板中相关的代码片段:

<div class="yourrating" id="t_{{ item.id }}"><span style="display:none;">{{score}}</span></div>

4

1 回答 1

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>​​​​​​​​​​​​​​
于 2012-10-30T16:41:53.743 回答