Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
¿Por qué mi código Javascript devuelve objeto objeto?

Soy nuevo en jQuery.

Intenté obtener/sumar algunos elementos de mis vistas de django en jQuery. Esto es lo que tengo:

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

Mi plantilla tiene esto:

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

Muestra Total :

Que alguien me ayude a hacerlo bien..

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

puede intentar usar django-mathfilter para este propósito, porque el usuario puede deshabilitar javascript y django-mathfilter es muy poderoso.

$ pip install django-mathfilters

Then add mathfilters to your INSTALLED_APPS.

entonces en tu plantilla puedes hacer algo como esto.

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

para obtener más información, lea esto https://pypi.org/project/django-mathfilters/

over 4 years ago · Santiago Trujillo Report

0

En lugar de involucrar el script js, recomendaré crear total_value en el lado de Django y moverlo a la plantilla.

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

No estoy seguro de qué camino seguir para renderizar la plantilla, pero debería mirar este

 # 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)
over 4 years ago · Santiago Trujillo Report

0

val() devuelve un atributo de valor como en <input type="text" value=something/> y html() devuelve el contenido (innerHTML) del elemento seleccionado.

Así que modifique sus códigos de esta manera y estará listo para comenzar. (Supongo que Django devuelve un valor numérico en {{ s_books.count }} y {{ 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 });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!