首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tally表列Jquery

Tally表列Jquery
EN

Stack Overflow用户
提问于 2021-11-22 19:23:20
回答 1查看 14关注 0票数 0

我有一个大约有10行和4列的标准样式表。我需要用户为每行选择列2、列3或列4(列2=5点、列3=3点和列4=0点)。

然后,我需要计算2-4列的总数。

我甚至连“选定的”部分都做不好,所以如果有任何帮助,我将不胜感激。

代码语言:javascript
复制
<table id="M3L1A1" class="table">
    <thead>
        <tr>
            <th scope="col">Points</th>
            <th scope="col">5 points each</th>
            <th scope="col">3 points each</th>
            <th scope="col">0 points each</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th scope="row">1.</th>
            <td class="col2"></td>
            <td class="col3"></td>
            <td class="col4"></td>
        </tr>
        <tr>
            <th scope="row">2.</th>
            <td class="col2"></td>
            <td class="col3"></td>
            <td class="col4"></td>
        </tr>
        <tr>
            <th scope="row">3.</th>
            <td class="col2"></td>
            <td class="col3"></td>
            <td class="col4"></td>
        </tr>
        <tr>
            <th scope="row">total</th>
            <td id="colTotal2"></td>
            <td id="colTotal3"></td>
            <td id="colTotal4"></td>
        </tr>
    </tbody>
</table>
<script>
$('#M3L1A1 tr').each(function(){
            $('td').click(function(){                   
                $('.selected').empty();
                $('td').removeClass('selected');
                $(this).addClass('selected');
                $('.selected').html('X');
        });
        return false;
    });
</script>
EN

回答 1

Stack Overflow用户

发布于 2021-11-22 19:42:44

您可以尝试如下所示:

代码语言:javascript
复制
var points = [5, 3, 0]
$('#M3L1A1 tr:not(:last) td').click(function() {
  $('.selected').empty();
  $(this).closest("tr").find("td").removeClass('selected');
  $(this).addClass('selected');
  $('.selected').html('X');

  $('#M3L1A1 tr:last td').text("")

  $('.selected').each(function() {
    var inx = $(this).index() - 1;
    var td = $('#M3L1A1 tr:last td').eq(inx)
    var point = +td.text() + points[inx];
    td.text(point)
  })

});

演示

代码语言:javascript
复制
var points = [5, 3, 0]
$('#M3L1A1 tr:not(:last) td').click(function() {
  $('.selected').empty();
  $(this).closest("tr").find("td").removeClass('selected');
  $(this).addClass('selected');
  $('.selected').html('X');

  $('#M3L1A1 tr:last td').text("")

  $('.selected').each(function() {
    var inx = $(this).index() - 1;
    var td = $('#M3L1A1 tr:last td').eq(inx)
    var point = +td.text() + points[inx];
    td.text(point)
  })

});
代码语言:javascript
复制
td {
  border: 1px solid black;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="M3L1A1" class="table">
  <thead>
    <tr>
      <th scope="col">Points</th>
      <th scope="col">5 points each</th>
      <th scope="col">3 points each</th>
      <th scope="col">0 points each</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1.</th>
      <td class="col2"></td>
      <td class="col3"></td>
      <td class="col4"></td>
    </tr>
    <tr>
      <th scope="row">2.</th>
      <td class="col2"></td>
      <td class="col3"></td>
      <td class="col4"></td>
    </tr>
    <tr>
      <th scope="row">3.</th>
      <td class="col2"></td>
      <td class="col3"></td>
      <td class="col4"></td>
    </tr>
    <tr>
      <th scope="row">total</th>
      <td id="colTotal2"></td>
      <td id="colTotal3"></td>
      <td id="colTotal4"></td>
    </tr>
  </tbody>
</table>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70071308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档