[英]How can i sum of multiple numbers with each function in jquery?
I want to sum of multiple numbers but this is unable to work with each function. 我想将多个数字相加,但这无法与每个函数一起使用。
What is tried:- 尝试了什么:-
totalPgCost(); function totalPgCost(){ var totalPgCost = 0; $('.pgBookingTable tr').find('.pgCost').each(function(){ totalPgCost += $(this).val(); }); $('.totalPgCost').text(totalPgCost); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <table class="pgBookingTable"> <tr><td><span class="pgCost">10000</span></td><tr> <tr><td><span class="pgCost">5000</span></td><tr> <tr><td>Total <span class="totalPgCost"></span> /-</td><tr> </table>
Why i'm getting 0? 为什么我得到0?
Answer will appreciated! 答案将不胜感激!
You need text
not val
. 您需要的
text
不是val
。 Also the html table is not properly balanced, as there was no closing tr
. html表也没有正确平衡,因为没有关闭
tr
。 Before adding the convert the string to number 在添加将字符串转换为数字之前
totalPgCost(); function totalPgCost() { var totalPgCost = 0; $('.pgBookingTable tr').find('.pgCost').each(function() { totalPgCost += parseInt($(this).text().trim(), 10); }); $('.totalPgCost').text(totalPgCost); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <table class="pgBookingTable"> <tr> <td><span class="pgCost">10000</span></td> </tr> <tr> <td><span class="pgCost">5000</span></td> </tr> <tr> <td>Total <span class="totalPgCost"></span> /-</td> </tr> </table>
You can also avoid find
if you are sure that only span will inside thar table will have that class 如果您确定只有span会在thar表中包含该类,也可以避免
find
totalPgCost(); function totalPgCost() { var totalPgCost = 0; $('.pgBookingTable .pgCost').each(function() { totalPgCost += parseInt($(this).text(), 10); }); $('.totalPgCost').text(totalPgCost); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <table class="pgBookingTable"> <tr> <td><span class="pgCost">10000</span></td> </tr> <tr> <td><span class="pgCost">5000</span></td> </tr> <tr> <td>Total <span class="totalPgCost"></span> /-</td> </tr> </table>
Use .text
使用
.text
$('.pgBookingTable tr').find('.pgCost').each(function(){
totalPgCost += parseInt($(this).text());
});
totalPgCost(); function totalPgCost(){ var totalPgCost = 0; $('.pgBookingTable tr').find('.pgCost').each(function(){ totalPgCost += parseInt($(this).text()); }); $('.totalPgCost').text(totalPgCost); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <table class="pgBookingTable"> <tr><td><span class="pgCost">10000</span></td><tr> <tr><td><span class="pgCost">5000</span></td><tr> <tr><td>Total <span class="totalPgCost"></span> /-</td><tr> </table>
you can directly use pgCost
for each loop and use text()
instead of val()
to get the text within span hope this helps 您可以在每个循环中直接使用
pgCost
,并使用text()
而不是val()
来获取跨度内的文本,希望这对您有所帮助
totalPgCost(); function totalPgCost(){ var totalPgCost = 0; $('.pgCost').each(function(){ totalPgCost += parseInt($(this).text()); }); $('.totalPgCost').text(totalPgCost); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <table class="pgBookingTable"> <tr><td><span class="pgCost">10000</span></td><tr> <tr><td><span class="pgCost">5000</span></td><tr> <tr><td>Total <span class="totalPgCost"></span> /-</td><tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.