0

只是一个小问题,我现在无法理解:

我有一个表格中显示的对象列表。对象值之一是分数。我可以使用 Django 模板标签将其显示为数字,但我想使用我的 jquery 插件来显示星星。不知道如何迭代这个。我正在尝试这个:

{% for result in mylist %}

<td>{{ result.type }}</td>
<td>{{ result.description }}</td>
<td>{{ result.rating.votes }}</td>

<td><div class="raty" data-number="{{ result.rating.score }}"></div></td>

{% endfor %}

再往下,我得到了这个:

<script>
$('.raty').raty({ readOnly: true, score: $('.raty').attr('value') });
</script>

问题是它使用 jquery 为每个对象显示相同的分数..

编辑:我得到了它的工作:

<script>
$('.raty').each(function() {
  $(this).raty({ readOnly: true, score: $(this).attr('data-number') });
});
</script>
4

1 回答 1

1

div元素没有属性value。尝试使用隐藏的输入元素。像这样的东西:

<table>
    {% for result in mylist %}
        <tr><td><input type='hidden' class='hidden_score' value='{{ result.rating.score }}'></input><div class="raty"></div></td></tr>
     {% endfor %}
</table>

然后在你的脚本中:

$.each($('.hidden_score'), function( index, value ) {
    var myval = $(this).val();
    $(this).parent().find( '.raty').raty({ readOnly:true, score:myval});
});

因此,对于每个具有 hidden_​​score 类的元素,您将获得它们的值,与属于每个元素的父元素(因此它们是兄弟姐妹)的元素具有正确的分数。

于 2014-02-13T20:36:49.893 回答