经过搜索,我还没有在网上找到我的jquery相关问题的解决方案。
我有一个页面上的多个(div class='rate')元素,我希望可以通过ajax更新。index.html:
{% for thing in thing_list %}
<div class='rate'><a href='{% url thing_rating %}'>Rate</a></div>
{% endfor %}它们引用的url类似于: urls.py
url(r'^(?P<object_id>\d+)/rate/(?P<score>[\d\-]+)/$', 'myview', name='thing_rating'),我在视图中检查request.is_ajax():view.py:
def myview(request):
if request.is_ajax():
// doing some other stuff here
return render_to_response('rate.html', context_instance=RequestContext(request))我的ajax需要帮助:
$( document ).ready( function() {
$('div#rate').click(function(){
$.ajax({
type: "POST",
url:"/????/",
data: { ????? },
success: function(data){ ????? },
error: function(){ alert("Error"); },
});
}如何引用上述jquery中的url?通过与我的urls.py中相同的正则表达式?
作为数据和成功函数,我应该传递什么?在我看来,所有繁重的工作都已经完成了。
谢谢你的点子!
发布于 2012-10-30 12:51:24
因为您返回的是呈现的模板。因此,我建议在ajax()函数中使用html,并将其附加到要显示它的dataType中。如果你有任何其他问题,请告诉我。
function vote(){
$('div#vote').click(function(e){
e.preventDefault();
var href = $(this).attr('href'); //Referring the anchor object which is clicked
$.ajax({
type: "POST",
url:href,
dataType: html,
success: function(data, status, xhr){ $('#div_to_load_html').html(data); },
error: function(){ alert("Error"); },
});
}https://stackoverflow.com/questions/13132531
复制相似问题