如何计算总和?

时间:2019-06-25 11:48:36

标签: javascript jquery

我有一个带复选框的数据表,我想计算使用jQuery的薪水的每天薪水,在我的控制台中第一行出现结果错误,而其他行则很好。

 <div id='form2'>
    <h3>form group 2</h3>
    <h4>DATE : <span id="dateE"></span></h4>
    <h4>chantier : <span id="ch"></span></h4>
    <h4>ouvrage : <span id="ou"></span></h4>
      <div class="form-group col-md-offset-5 ">
        <button class="btn btn-success add-all" type="submit" id="hide">Pointage</button>
      </div>
   <table class="table table-bordered" id="mytable">
        <tr>
            <th>Archive</th>
            <th><input type="checkbox" id="check_all"></th>
            <th>S.No.</th>
            <th>matricule</th>
            <th>nom & prenom</th>
            <th>salaire net</th>
            <th>nbre de jour </th>
            <th>prime</th>
        </tr>
        @if($salaries->count())
            @foreach($salaries as $key => $salarie)
                <tr id="tr_{{$salarie->id}}">
                  <td>archive</td>
                  <td><input type="checkbox" class="checkbox" data-id="{{$salarie->id}}"></td>
                  <td>{{ ++$key }}</td>
                  <td>{{ $salarie->matricule }}</td>
                  <td >{{ $salarie->nom }} {{ $salarie->prenom }}</td>
                  <td><input type="hidden" name="salaire" value="{{ $salarie->salairenet }}">{{ $salarie->salairenet }}</td>
                  <td ><input type="text" class='input2' name="nbreJ" class="form-control" ></td>
                  <td><input type="text" name="prime" class="form-control" value="0"></td>
                </tr>
            @endforeach
        @endif
    </table>
 </div>

jquery代码,除SUM之外,所有值均准确显示

$("tr").each(function(i, r) {
  if (i > 0 && $(r).find("input").first().prop("checked")) {
    let s = parseInt($(r.cells[5]).text());
    let value = $('#nbr').val();
    let prime = $(r.cells[7]).find('input').val();
    let sum = (((s) / 24) * value) + prime;

    items.push({
      s,
      value,
      prime,
      sum
    });

  }
  console.log(items);
});

2 个答案:

答案 0 :(得分:2)

您需要在val上使用parseInt。如果没有parseInt,则将其视为字符串,并尝试concat两个字符串而不计算数字。从输入值中删除parseInt,然后查看对代码段的影响。

$( "p" ).click(function() {
let s = 24;//parseInt($(r.cells[5]).text());
let value = parseInt($('#nbr').val());
let prime = parseInt($('#test').val());//5;//$(r.cells[7]).find('input').val();
let sum = (((s)/24) * value ) + prime;
alert(sum);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='text' id='nbr'/>
<input type='text' id='test'/>
<p>Click for test</p>

答案 1 :(得分:0)


var rows = $(document).find('tr[id^="tr_"]');
var data = [];
 rows.each(function(){
   var row = $(this);
   var checkbox = row.find('[type=checkbox]');
   if(checkbox.is(':checked')){
     var nbr = row.find('#nbreJ').val();
     var prime = row.find('[name=prime]').val();
     var salary = row.find('[name=salaire]').val();
     var sum = salary / 24 * nbr + prime;
     data.push({nbr, prime, salary, sum});

   }

 })
console.log(data);