I'm trying to load an external page which contains a table like this:
<table id="uniquedatatable"&g开发者_开发百科t;
<tr class="row">
<td class="num col"><a> 1 </a></td>
<td class="str col"><a>Text</a></td>
<td class="num col"><a> 7</a></td>
<td class="str col"><a>Text</a></td>
</tr>
</table>
*Notice the whitespace's in the class names of the td's and around the numbers (1,7).
I need to find all cell's (td's) with class "num col", get the numeric value between the a tag'a and sum all the values. Last I need to display this value on my own page.
So the result I would get from the above table should be: 1 + 7 = 8.
I've gotten this far:
<script src="http://code.jquery.com/jquery-1.5.1.min.js" type="text/javascript"></script>
<p id="result"></p>
<script type="text/javascript">
var url = "http://someurl";
var sum = 0;
$('#result').load('url #uniquedatatable tr td.num col a', function() {
sum += Number($(this).text());
});
_gel('result').innerHTML = sum;
</script>
Realizing something is wrong (close to being correct I hope :)), I need help connecting the "dots" here so that I can:
Calculate the sum (this is the "big" problem :S)
Write the result back.
Any help is appreciated.
Try this:
<script type="text/javascript">
var url = "<LOCAL_URL>";
var sum = 0;
$('#result').load(url, function() {
$("#uniquedatatable tr td.num.col a", "#result" ).each(function(){
sum += parseInt($(this).text(), 10);
});
_gel('result').innerHTML = sum;
});
</script>
for calc you can use sum += parseInt($(this).text());
精彩评论