我是 jQuery 新手。
我尝试在 jQuery 中从我的 django View 中获取/总结一些项目。这是我的:
$(document).ready(function()
{
var sch = $('#sch-books');
var gov = $('#gov-books');
var total = sch.val() + gov.val();
$('#total').text("Total : " + total);
});
我的模板是这样的:
<div id="sch-books" class="h6 mb-1">School copies - <b>{{ s_books.count }}</b></div>
<div id="gov-books"class="h6 mb-1">Govt copies - <b>{{ g_books.count }}</b></div>
<div id="total"></div>
它显示 Total :
谁能帮我把它弄好..
最佳答案
我建议在 Django 端创建 total_value 并将其移至模板,而不是涉及 js 脚本。
<div class="h6 mb-1" data-count="{{ s_books.count }}">School copies - <b>{{ s_books.count }}</b></div>
<div class="h6 mb-1" data-count="{{ g_books.count }}">Govt copies - <b>{{ g_books.count }}</b></div>
<div>{total_count}</div>
我不确定你按照哪种方式呈现模板,但应该看看这个
# views.py
from django.shortcuts import render
def render_users(request):
g_books = {}
s_books = {}
context = {
"g_books": g_books,
"s_books": s_books,
"total_count": g_books.count + s_books.count
}
return render(request, 'books.html', context)
关于javascript - 为什么我的 Javascript 代码返回 object 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70045185/