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

313
Views
Si el valor en la clase de precio en HTML es igual a una cadena, ¿cómo se puede descartar y no contar y sumar el resto de los valores de tipo Número?

Tengo este código, pero quiero agregarlo en caso de que el valor en el campo de precio sea igual a una cadena, ¿cómo se puede descartar este valor y no contar y sumar el resto de los valores de tipo número?

 var theSummry = 0; $(".price").each(function() { theSummry += parseInt($(this).text()); }) $(".the-total").text(theSummry);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <table style="width:70%;text-align: center;margin: 30px auto;"> <tr> <th>Person 1</th> <th>Person 2</th> <th>Person 3</th> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td>Linus</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">10</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">qwe</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">10</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">30</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">10</td> </tr> <tr class="the-total"></tr> </table>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Para lograr esto, puede fusionar la salida de NaN al intentar parseInt() una cadena a un valor 0 usando el operador lógico OR:

 var theSummry = 0; $(".price").each(function() { theSummry += parseInt($(this).text(), 10) || 0; }) $(".the-total").text(theSummry);
 table { width: 70%; text-align: center; margin: 10px auto; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <table> <tr> <th>Person 1</th> <th>Person 2</th> <th>Person 3</th> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td>Linus</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">10</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">qwe</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">10</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">30</td> </tr> <tr> <td>Emil</td> <td>Tobias</td> <td class="price">10</td> </tr> <tr class="the-total"></tr> </table>

about 4 years ago · Juan Pablo Isaza 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!