<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<table border="1" cellspacing="0">
<thead>
<th></th>
<th>语文</th>
<th>数学</th>
<th>英语</th>
<th>总分</th>
</thead>
<tbody>
<tr>
<td></td>
<td><input type="text" placeholder="--" onkeyup="total()"></td>
<td><input type="text" placeholder="--" onkeyup="total()"></td>
<td><input type="text" placeholder="--" onkeyup="total()"></td>
<td id="row1"></td>
</tr>
<tr>
<td></td>
<td><input type="text" placeholder="--" onkeyup="total()"></td>
<td><input type="text" placeholder="--" onkeyup="total()"></td>
<td><input type="text" placeholder="--" onkeyup="total()"></td>
<td></td>
</tr>
<tr>
<td>合计</td>
<td id="col1"></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<script src="js/jquery.min.js"></script>
<script>
function total(){
var colTotal = 0;
var rowTotal = 0;
// 计算列和
$('table tr:gt(0)').each(function(){
if(parseFloat($(this).find('td:eq(1) input[type="text"]').val())){
colTotal += parseFloat($(this).find('td:eq(1) input[type="text"]').val());
}
});
$('#col1').text(colTotal);
// 计算行和
$('table tr:eq(1) td input[type="text"]').each(function(){
if(parseFloat($(this).val())){
rowTotal += parseFloat($(this).val());
}
});
$('#row1').text(rowTotal);
}
</script>
</body>
</html>
网友评论