2017-09-01 76 views
0

我有數量單元格爲可修改時,它需要與其他父母的td值相乘時可編輯表格。這裏是我的代碼
(即)如果我將數量更改爲2,則父行需要乘以2乘以表td值

$(document).ready(function(){ 
 
    $('.quantity').on('change, keyup',function(){ 
 
\t \t var tbvalue=$(this).parents('td').val(); 
 
    console.log(tbvalue); 
 
    var val=$(this).val(); 
 
    var result=tbvalue*val; 
 
    console.log(result); 
 
    }); 
 
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<div class="container"> 
 
    <h2>Table calculation</h2> 
 
    <p>Calculaton</p>    
 
    <table class="table table-hover"> 
 
    <thead> 
 
     <tr> 
 
     <th>value1</th> 
 
     <th>value2</th> 
 
     <th>value3</th> 
 
     <th>Quantity</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr> 
 
     <td>10</td> 
 
     <td>5</td> 
 
     <td>4</td> 
 
     <td class="quantity" type="number" contenteditable>1</td> 
 
     </tr> 
 
     <tr> 
 
     <td>8</td> 
 
     <td type>2</td> 
 
     <td>3</td> 
 
     <td class="quantity" type="number" contenteditable>1</td> 
 
     </tr> 
 
     <tr> 
 
     <td>20</td> 
 
     <td>3</td> 
 
     <td>5</td> 
 
     <td class="quantity" type="number" contenteditable>1</td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</div>

+0

'變種tbvalue = $(本)的.text();' –

回答

4

使用該$(this).text();嘗試從每個TD this.Get文本並乘以與輸入值

$(document).ready(function(){ 
 
    $('.quantity').on('change, keyup',function(){ 
 
     var val=$(this).text(); 
 
if(val =='' || isNaN(val) || val < 1){ 
 
return; 
 
} 
 
     $(this).siblings().each(function(){ 
 
      var tbvalue=$(this).text(); 
 
      
 
      var result= parseInt(tbvalue)*parseInt(val); 
 
      
 
      $(this).text(result); 
 
     }) 
 

 

 
    
 
    }); 
 
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<div class="container"> 
 
    <h2>Table calculation</h2> 
 
    <p>Calculaton</p>    
 
    <table class="table table-hover"> 
 
    <thead> 
 
     <tr> 
 
     <th>value1</th> 
 
     <th>value2</th> 
 
     <th>value3</th> 
 
     <th>Quantity</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr> 
 
     <td>10</td> 
 
     <td>5</td> 
 
     <td>4</td> 
 
     <td class="quantity" type="number" contenteditable>1</td> 
 
     </tr> 
 
     <tr> 
 
     <td>8</td> 
 
     <td type>2</td> 
 
     <td>3</td> 
 
     <td class="quantity" type="number" contenteditable>1</td> 
 
     </tr> 
 
     <tr> 
 
     <td>20</td> 
 
     <td>3</td> 
 
     <td>5</td> 
 
     <td class="quantity" type="number" contenteditable>1</td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</div>

+0

的數量值也越來越計算如何解決它 –

+1

@HarishKarthick沒有也不會如果在編輯領域的工作時間你清空這個值,然後去改變。它會生成NAN總是 –

+1

@AlivetoDie他必須爲此寫入驗證。反正我已經更新了答案。 – XYZ