javascript - 添加并显示表中列总和的结果
问题描述
再会。我是这方面的初学者,我有一个包含以下记录的表格,我想添加红队、黄队和绿松石设备列,并在表格顶部的标签中显示总和
为此,我将我的 html 代码附加到 jsp 和参考图像 分数表中
<div class="tabs-body">
<div class="tabs-item" id="tab1">
<div class="container">
<div class="con-contador">
<label>Puntaje Rojo :</label>
<label id="projo" class="contador"></label>
<label >Puntaje Amarillo :</label>
<label id="pamarillo" class="contador"></label>
<label>Puntaje Turqueza :</label>
<label id="pturqueza" class="contador"></label>
<a href="reportes.jsp" target="_blank" class="btnreporte">Reporte</a>
</div>
<div class="table-responsive-vertical" >
<table id="reporte" class="table table-bordered table-striped table-hover table-mc-light-blue">
<thead>
<tr>
<th>Disciplina</th>
<th>Categoria</th>
<th>Genero</th>
<th style="background-color: red">Equipo Rojo</th>
<th style="background-color: yellow">Equipo Amarillo</th>
<th style="background-color: turquoise">Puntaje Turqueza</th>
</tr>
</thead>
<tbody>
<%
for (int i=0; i < v.size();i++){
puntaje p = (puntaje)v.elementAt(i);
%>
<tr>
<td data-title="Disciplina"><%=p.getNombredeporte()%></td>
<td data-title="Categoria"><%=p.getCategoria() %></td>
<td data-title="Genero"><%=p.getGenero() %></td>
<td data-title="Puntaje"><%=p.getPuntaje1() %></td>
<td data-title="Puntaje"><%=p.getPuntaje2() %></td>
<td data-title="Puntaje"><%=p.getPuntaje3() %></td>
</tr>
<%
}
%>
</tbody>
</table>
</div>
</div>
</div>
</div>
我知道可以使用 javascript 中的函数来完成,但我仍然是新手,我请求您的支持。
解决方案
Firstly I'd say that JSP is Java Server Pages, not javascript. JSP code gets run (for example, generating puntaje sums) on the server before the webpage is returned to the web browser. In contrast, javascript code is executed after the webpage is returned to the web browser. Since you already have JSP code, why not just run your code server-side? Perhaps another "for" loop that runs to generate sums before your elements. That said, if you really want to use javascript then the general procedure will be: add another data-attribute to your points TDs, like
<td data-puntaje-rojo=<%=p.getPuntaje1() %>>
Add classed spans to your labels like:
<label>Puntaje Rojo : <span class="puntaje-sum-rojo"></span></label>
Then in your javascript, use some form of selector to iterate over a collection/array of the TDs and store the sum of all data-puntaje-rojo values. Then, use javascript or jquery to inject the appropriate sum value into the appropriate label span.
While best practice is to store your javascript code in an external file on your web server website location, you can also write javascript directly inside of your JSP page, the javascript code must be inside tags. And, it's best to have your javascript code run after your HTML is fully loaded: javascript and jquery both provide ways to accomplish that.
推荐阅读
- angular - 如何对齐不同行上的 2 个 mat-slide-toggle 字段
- c - 是否有可以输出长值的 longjmp 版本?
- angular - 特定于每个模块的注入
- wordpress - 将wordpress应用程序从本地移动到cpanel后如何更改旋转滑块的baseurl
- matlab - 为什么我使用 Matlab 滤波器设计工具创建的滤波器不适用于定点数?
- integer - 每次计数器更新(可变整数范围)如何发送一点?
- github - 仅当有人打开 PR 时才运行 GitHub Action
- excel - 使用一张表中的单元格值作为粘贴到另一张表的目的地
- c# - 如何使用 wpf 在页面之间导航
- wordpress - 重置密码后如何注销用户