我是 jquerry 的新手。我嘗試從 jquerry 的 django 視圖中獲取/總結一些專案。這就是我所擁有的
$(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 :
有人可以幫我做對..
uj5u.com熱心網友回復:
前言:我建議您在模板中處理此問題,現在有兩個答案向您展示如何處理(此處和此處)。(我不做 Django。)請注意,在客戶端代碼中執行此操作會導致<div id="total"></div>非常短暫地顯示為空白,然后用數字填充。如果頁面加載時該元素在視圖中,用戶將看到閃爍——即使他們無法準確看到閃爍的是什么(這可能會令人惱火)。通過在 Django 視圖中執行此操作來避免這種情況可能會更好。
但是重新您的客戶端代碼:
div元素沒有value,它們有textContent和innerHTML(有關詳細資訊,請參閱此問題的答案)。但是div除了您要查找的數字之外,這些元素的內容中還有很多東西。雖然您可以將數字從文本中分離出來(或僅從 中的b元素中檢索它div),但這些都是脆弱的解決方案,如果有人以看似無辜的方式更改模板,它們就會崩潰。
如果您真的想用客戶端代碼處理這個問題,請將您想要的值存盤data-*在div元素的屬性中:
<div id="sch-books" class="h6 mb-1" data-count="{{ s_books.count }}">School copies - <b>{{ s_books.count }}</b></div>
<div id="gov-books" class="h6 mb-1" data-count="{{ g_books.count }}">Govt copies - <b>{{ g_books.count }}</b></div>
<div id="total"></div>
然后獲取該data-*屬性:
$(document).ready(function() {
const sch = $("#sch-books").attr("data-count");
const gov = $("#gov-books").attr("data-count");
const total = sch gov;
$("#total").text(`Total : ${total}`);
});
uj5u.com熱心網友回復:
我會建議在 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)
uj5u.com熱心網友回復:
為此,您可以嘗試使用 django-mathfilter。因為 javascript 可以被用戶禁用,并且 django-mathfilter 非常強大。
$ pip install django-mathfilters
Then add mathfilters to your INSTALLED_APPS.
然后在你的模板中你可以做這樣的事情。
{% load mathfilters %}
........
{% with s_books.count as s_book and g_books.count as g_book %}
<div id="sch-books" class="h6 mb-1">School copies - <b>{{ s_book }}</b></div>
<div id="gov-books"class="h6 mb-1">Govt copies - <b>{{ g_book }}</b></div>
<div id="total">{{ s_book|add:g_book }}</div>
{% endwith %}
有關更多資訊,請閱讀此https://pypi.org/project/django-mathfilters/
uj5u.com熱心網友回復:
val()回傳類似 in 的value屬性<input type="text" value=something/>并html()回傳所選元素的內容(innerHTML)。
所以像這樣修改你的代碼,你就可以開始了。(我假設 django 在{{ s_books.count }}and 中回傳一個數值{{ g_books.count }})。
$(document).ready(function()
{
var sch = $('#sch-books b').html(); // added b element and calling html()
var gov = $('#gov-books b').html(); // added b element and calling html()
var total = parseInt(sch) parseInt(gov); // converted the string into number using parseInt()
$('#total').text("Total : " total); // worked
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/361098.html
標籤:javascript 查询 姜戈
