简体   繁体   English

为什么我的 Javascript 代码返回 object object?

[英]Why does my Javascript code return object object?

I am new to jquerry.我是 jquery 的新手。 I tried getting/summing some items from my django views in jquerry.我尝试从 jquery 中的 django 视图中获取/汇总一些项目。 this is whhat I have这就是我所拥有的

$(document).ready(function()
{
    var sch = $('#sch-books');
    var gov = $('#gov-books');
    var total = sch.val() + gov.val();
    $('#total').text("Total : " + total); 

});

My template has these我的模板有这些

<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>

It displays Total: May someone help me get it right..它显示Total:有人可以帮我把它弄好..

you can try to use django-mathfilter for this purpose.because javascript can be disabled by the user and django-mathfilter is so powerfull.您可以尝试为此目的使用 django-mathfilter。因为 javascript 可以被用户禁用并且 django-mathfilter 非常强大。

$ pip install django-mathfilters

Then add mathfilters to your INSTALLED_APPS.

then in you template you can just do something like this.然后在你的模板中你可以做这样的事情。

{% 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 %}

for more information read this https://pypi.org/project/django-mathfilters/欲了解更多信息,请阅读此https://pypi.org/project/django-mathfilters/

Instead of involving the js script, I will recommend creating total_value on the Django side and moving it to the template.我建议不要使用 js 脚本,而是在 Django 端创建 total_value 并将其移动到模板中。

<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>

I am not sure which way you follow to render the template, but should look at this one我不确定您采用哪种方式来呈现模板,但应该看看这个

# 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)

val() returns value attribute like in <input type="text" value=something/> and html() returns the content (innerHTML) of selected element. val()返回属性,如<input type="text" value=something/>html()返回所选元素的内容(innerHTML)

So modify your codes like this and you are good to go.所以像这样修改你的代码,你对 go 很好。 (I am assuming that django returns a numeric value in {{ s_books.count }} and {{ g_books.count }} ). (我假设 django 在{{ s_books.count }}{{ 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

});

div elements don't have a value , they have textContent and innerHTML (see this question's answers for details). div元素没有value ,它们有textContentinnerHTML (有关详细信息,请参阅此问题的答案)。

But the contents of those div elements will have lots of things in them other than the numbers you're looking for.但是这些div元素的内容中除了您要查找的数字之外还有很多东西。 While you could isolate the number from the text (or retrieve it just from the b element within the div ), those are both fragile solutions that will break if someone changes the template in seemingly-innocent ways.虽然您可以从文本中分离出数字(或仅从div中的b元素中检索它),但如果有人以看似无辜的方式更改模板,这些都是脆弱的解决方案。

I would suggest you handle this in the template, but sadly I don't do Django so I can't point to how to do that.我建议你在模板中处理这个,但遗憾的是我不做 Django 所以我不能指出如何去做。 (I had hoped you could do {{ s_books.count + g_books.count }} but this suggests to me you can't, which seems surprising.) (我曾希望你能做到{{ s_books.count + g_books.count }} 但这告诉我你不能,这似乎令人惊讶。)

If you really want to handle this with client-side code, store the values you want in data-* attributes on the div elements:如果您真的想使用客户端代码处理此问题,请将所需的值存储在div元素的data-*属性中:

<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>

Then grab that data-* attribute:然后抓住那个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}`); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Javascript返回[object HTMLScriptElement]而不是预期的文本? - Why does my Javascript return [object HTMLScriptElement] instead of expected text? 为什么我的 HTML 代码返回 [object HTMLFormElement]? - Why does my HTML code return [object HTMLFormElement]? 为什么以下代码未初始化我的JavaScript对象? - Why does the following code not initialize my JavaScript object? 为什么内容在 JavaScript 上返回相同的对象? - Why does the content return the same object on JavaScript? 为什么对象中的函数返回未定义? - Why does a function in my object return undefined? JavaScript中的Object.create:为什么我的代码输出对象而不是名称字符串值 - Object.create in javascript : why does my code outputs Object instead of name string value 为什么我的对象方法没有注册? 使用Javascript - Why does my object method not register? Javascript 为什么我的代码不能与球员的目标工作? - Why does my code not work with a player object? 为什么我的JS应该返回一个对象返回undefined? - Why does my JS that's supposed to return an object return undefined? 如果我在 JavaScript 中使用 let 或 const,为什么我的 function 无法返回/初始化工厂 function object? - Why does my function fail to return/initialize a factory function object if I use let or const in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM