How to get value of td elemets on JQuery?
I have table 开发者_如何学Cwith numbers tht needs to be summarize. Example:
<td class="mcost_el.entry[0]">93,08</td>
<td class="mcost_el.entry[1]">544,33</td>
How get all values of td elements with class mcost_el.entry[]
and summarize it?
For inputs i'm use $('#d1').attr("value");
. Is suitable for this task? (by adding value="" for td)
You can use .text()
rather than what you have suggested.
Update your HTML to
<td class="mcost_el">93,08</td>
<td class="mcost_el">544,33</td>
Add in javascript for
var _total = 0;
$(function(){
$('.mcost_el').each(function(){
_total += parseFloat($(this).text()); // will give you the value.
});
});
Example http://jsfiddle.net/tqH4y/
Also, rather than using .attr("value")
in the future, you can just use .val()
var tdtotal = 0;
$("td[class^='mcost_el.entry[']").each(function() {
tdtotal += parseInt($(this).text().replace(",",""));
})
alert(tdtotal);
Working demo - http://jsfiddle.net/ipr101/vnXCr/2/
I'm not quite sure i'm answering correctly here, its unclear if you are seeking a way to get text from a TD element, or wether your having trouble targetting all td's with a certain class (selector issues)
.attr("value")
or better .val()
is for input fields
if you want to get text from a TD element, you should be using:
$('td').text();
精彩评论