dongpo1216 2016-02-19 07:34
浏览 89
已采纳

添加多个动态行后添加动态行的值不起作用

I've created a dynamic table where users have to enter the student name and their subject and marks. Each student can contain several subjects. I have attached the code below, which would be easier to understand.

Click here to view the working code

What is working :

The sum is working perfectly fine for 1 block of students that is added.

What I need

The sum should add for each block of students that I dynamically add and dispaly the totals in sub-total. The sub-totals should add to give the total.

Code

function calculateSum() {
    var sum = 0;
    $(".marks").each(function () {

        if (!isNaN(this.value) && this.value.length != 0) {
            sum += parseFloat(this.value);
        }

    });
    $("#Sub").val(sum.toFixed(2));
    $("#Sub1").val(sum.toFixed(2));
}

function recalc() {
    var tt = 0;
    $("#tb").find('tr').each(function () {
        $(this).find('input.row-total').val(dateTotal ? dateTotal : "");
        tt += isNumber(dateTotal) ? dateTotal : 0;
    }); //END .each
    $("#table-grand-total").html(tt);
}

function calculateTotal() {
    var sum = 0;
    //iterate through each textboxes and add the values
    $(".row-total").each(function () {
        //add only if the value is number
        if (!isNaN(this.value) && this.value.length !== 0) {
            sum += parseFloat(this.value);
        }
    });
    //.toFixed() method will roundoff the final sum to 2 decimal places
    $(".table-total").val(sum.toFixed(2));
}
  • 写回答

1条回答 默认 最新

  • dongtuoleng8624 2016-02-19 15:01
    关注

    You ve some problems in your code:

    • You re using ids while working with dynamic items, use class instead
    • table footer goes outside the table body
    • i always avoid cloning elemets in dynamic tables, you can create tamplates instead
    • event bindings

    look at my example:

    $(function() {
      var students = 0;
    
      function studentTemplate(students) {
    
        return '<tr class="student_name"> <td colspan="2"><b>Student Name</b></td><td colspan="1"> <input type="text" name="student_names[]" class="form-control" placeholder="Student Name"> </td><th><a style="font-size:18px;width:33%;" class="newstudent" title="Add More Person"><span class="glyphicon glyphicon-plus"></span></a></th> </tr><tr class="student_marks"> <td><b>Subject Name and Marks</b></td><td> <input type="text" name="subnames[]" class="form-control" placeholder="Subject Name"> </td><td> <input type="number" name="marks[]" class="marks student_' + students + ' form-control" student_ref="student_' + students + '" placeholder="Marks"> </td><th><a style="font-size:18px;width:33%;" class="addsubject" ><span class="glyphicon glyphicon-plus"></span></a></th> </tr><tr class="subtotal"> <td colspan="2"><b>Sub - Total</b></td><td> <input type="text" name="subtotals[]" class="row-total student_' + students + ' form-control" disabled> </td></tr>';
    
      }
    
      function subTemplate(students) {
        return '<tr class="student_marks"> <td><b>Subject Name and Marks</b></td><td> <input type="text" name="subnames[]" class="form-control" placeholder="Subject Name"> </td><td> <input type="number" name="marks[]" class="marks student_' + students + ' form-control" student_ref="student_' + students + '" placeholder="Marks"> </td><th><a style="font-size:18px;width:33%;" class="addsubject" ><span class="glyphicon glyphicon-plus"></span></a></th> </tr>';
    
      }
    
      $('#tb tbody').append(studentTemplate(students));
    
      $('#tb tbody').on("click", "a.newstudent", function() {
        students++;
        $('#tb tbody').append(studentTemplate(students));
      });
    
      $('#tb tbody').on("click", "a.addsubject", function() {
        $(this).closest(".student_marks").after(subTemplate(students));
      });
    
      $('#tb tbody').on("keyup", "input.marks", function() {
        subtotal($(this).attr("student_ref"));
      });
    
      function subtotal(ref) {
        var sub = 0;
        $(".marks." + ref).each(function() {
          sub += $(this).val() != "" ? parseInt($(this).val()) : 0;
        });
        $(".row-total." + ref).val(sub);
        calcTotal();
      }
    
      function calcTotal() {
        var total = 0
        $(".row-total").each(function() {
          total += $(this).val() != "" ? parseInt($(this).val()) : 0;
        })
    
        $('#table-grand-total').val(total);
    
      }
    
    });
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="panel panel-primary">
      <div class="bs-example">
        <div class="Container">
          <table class="table table-hover small-text" style="margin-top: 50px;" id="tb" border="1px">
            <tbody>
            </tbody>
            <tfoot>
              <th colspan="2">GRAND TOTAL</th>
              <td>
                <input type="text" name="GrandTotal[]" id="table-grand-total" class="table-total form-control" disabled>
              </td>
            </tfoot>
          </table>
        </div>
      </div>
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败