Django模板标签/ jquery for-迭代? [英] Django template tag / jquery for- iteration?
问题描述
只是一个小问题,我现在不能让我的头脑:
我有一个表中列出的对象。其中一个对象值是一个分数。我可以使用Django模板标签将其显示为数字,但是我想使用我的jQuery插件来显示星号。不知道如何迭代这个。我正在尝试:
{%for mylist%}
< td> {{result.type}}< / td>
< td> {{result.description}}< / td>
< td> {{result.rating.votes}}< / td>
< td>< div class =ratydata-number ={{result.rating.score}}>< / div>< / td>
{%endfor%}
进一步下来我得到这个: / p>
< 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>
A div
元素没有值
属性。尝试使用隐藏的输入元素。这样的一个例子:
< table>
{%for mylist%}
< tr>< td>< input type ='hidden'class ='hidden_score'value ='{{result.rating.score}}' >< / input>< div class =raty>< / div>< / td>< / tr>
{%endfor%}
< / table>
然后在您的脚本中:
<$ p $($('。hidden_score'),function(index,value){
var myval = $(this).val();
$(这个).parent()。find('.raty')。raty({readOnly:true,score:myval});
});
因此,对于每个具有hidden_score类的元素,您将获得其值为 这些元素属于每个元素的父母(所以他们是兄弟姐妹),具有正确的分数。
Just a small problem i can't get my head around right now:
I have a list of objects presented in a table. One of the objects values is a score. I can use a Django template tag to show this as a number but i want to use my jquery plug-in to show stars instead. Not sure how to iterate over this. I'm trying this:
{% 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 %}
And further down i got this:
<script>
$('.raty').raty({ readOnly: true, score: $('.raty').attr('value') });
</script>
The problem is that it shows the same score for every object with the jquery..
EDIT: I got it to work with this:
<script>
$('.raty').each(function() {
$(this).raty({ readOnly: true, score: $(this).attr('data-number') });
});
</script>
A div
element does not have a value
attribute. Try using a hidden input element. Something like this:
<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>
And then in your script:
$.each($('.hidden_score'), function( index, value ) {
var myval = $(this).val();
$(this).parent().find( '.raty').raty({ readOnly:true, score:myval});
});
So, for each of elements having a class of hidden_score, you will get their value make a "raty" with the elemens belonging to the parent of each element (so they are siblings) with the correct score.
这篇关于Django模板标签/ jquery for-迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!