2014-01-28 2 views
0

열의 값을 뺄 표가 있습니다. 그러나 값을 빼는 대신 합계가 계속됩니다.비슷한 클래스 이름을 가진 테이블에서 값 빼기

jsFiddle.

HTML :

<table> 
    <tr class="parent-ca"> 
     <td> 
      <h4><em>Contract Amount</em></h4> 

     </td> 
     <td> 
      <input type="text" class="rr" /> //all rr should be subtracted 
     </td> 
     <td> 
      <input type="text" class="rr1" /> //all rr1 should be subtracted 
     </td> 
     <td> 
      <input type="text" class="rr2" /> 
     </td> 
     <td> 
      <input type="text" class="rr3" /> 
     </td> 
    </tr> 
    <tr class="parent-abc"> 
     <td> 
      <h5><em>Contract Amount</em></h5> 

     </td> 
     <td> 
      <input type="text" class="rr" /> 
     </td> 
     <td> 
      <input type="text" class="rr1" /> 
     </td> 
     <td> 
      <input type="text" class="rr2" /> 
     </td> 
     <td> 
      <input type="text" class="rr3" /> 
     </td> 
    </tr> 
    <tr class="parent-a"> 
     <td> 
      <h5><em>Contract Amount</em></h5> 

     </td> 
     <td> 
      <input type="text" class="rr" /> 
     </td> 
     <td> 
      <input type="text" class="rr1" /> 
     </td> 
     <td> 
      <input type="text" class="rr2" /> 
     </td> 
     <td> 
      <input type="text" class="rr3" /> 
     </td> 
    </tr> 
    <tr class="parent-b"> 
     <td> 
      <h5><em>Contract Amount</em></h5> 

     </td> 
     <td> 
      <input type="text" class="rr" /> 
     </td> 
     <td> 
      <input type="text" class="rr1" /> 
     </td> 
     <td> 
      <input type="text" class="rr2" /> 
     </td> 
     <td> 
      <input type="text" class="rr3" /> 
     </td> 
    </tr> 
    <tr class="parent-c"> 
     <td> 
      <h5><em>Contract Amount</em></h5> 

     </td> 
     <td> 
      <input type="text" class="rr" /> 
     </td> 
     <td> 
      <input type="text" class="rr1" /> 
     </td> 
     <td> 
      <input type="text" class="rr2" /> 
     </td> 
     <td> 
      <input type="text" class="rr3" /> 
     </td> 
    </tr> 
    <tr class="nettotal tt table-sub" data-minus="ca,contract,other2"> 
     <td> 
      <h4>Net Cash</h4> 

     </td> 
     <td> 
      <input type="text" id="rr" /> 
     </td> 
     <td> 
      <input type="text" id="rr1" /> 
     </td> 
     <td> 
      <input type="text" id="rr2" /> 
     </td> 
     <td> 
      <input type="text" id="rr3" /> 
     </td> 
    </tr> 
</table> 

jQuery를 :

function calcSubMTotal(p) { 
    var minus = 0; 
    var minus_total = $('#' + p + ''); 


    $('.' + p + '').each(function (i) { 
     if (!isNaN(this.value) && this.value.length !== 0) { 
      minus -= this.value; //trying to subtract but this just adds up rather than subtract 
     } 
    }); 

    minus_total.val(minus); 


} 

$('input[type=text]').on('keyup', function() { 
    calcSubMTotal("rr"); 
    calcSubMTotal("rr1"); 
    calcSubMTotal("rr2"); 
    calcSubMTotal("rr3"); 
}); 
+2

귀하의 코드는 실제로 최선입니다 g 올바른 것, 즉 연속적으로 빼는 것. 문제는 무엇입니까? –

+0

@RajaprabhuAravindasamy, 나는 값을 뺀 값을 얻고 싶습니다. 예를 들어, 첫 번째 행 값은 50000이고, 다음 값이 4000 인 경우 합계는 54000이 아니라 45000입니다. 3000을 입력하면이 값은 57000이 아닌 42000이되어야합니다. – input

+0

@Anton, 음수로 입력하지 않습니다. – input

답변

1

FIDDLE

업데이트 JQuery와

 function calcSubMTotal(p) { 
    var minus = 0; 
    var isFirst=true; 
    var minus_total = $('#' + p + ''); 


    $('.' + p + '').each(function (i) { 
     if (!isNaN(this.value) && this.value.length !== 0) { 
      if(!isFirst) 
      minus -= parseInt(this.value); 
      else 
      { 
       minus=parseInt(this.value); 
       isFirst=false; 
      } 
     } 
    }); 

    minus_total.val(minus); 


} 

$('input[type=text]').on('keyup', function() { 
    calcSubMTotal("rr"); 
    calcSubMTotal("rr1"); 
    calcSubMTotal("rr2"); 
    calcSubMTotal("rr3"); 
}); 
+0

고맙습니다. – input

관련 문제