jquery - Alert sum result based on row and class -


i'm trying sum text value inside table. here try

$(document).on("change", ".kd1", function() {  					 var sum = 0;  						$('.kd1').each(function() {  							sum += number($( ).closest('.kd1').val());  						})  						alert(sum);  				});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>    <table>  	<tr>  		<td><input type="text" class="kd1"></td>  		<td><input type="text" class="kd1"></td>  	</tr>  	<tr>  		<td><input type="text" class="kd1"></td>  		<td><input type="text" class="kd1"></td>  	</tr>  	  </table>

for now, i'm able alert it. but result total of each tr want separate result each row. how can achieve ? in advance

sorry bad english.

it not seem user-friendly present sums in alert. instead add column table, , display sums there.

to calculate sums per row, add outer loop on rows, , calculate sum row inside loop. suggest using reduce getting sum, detail. see work in snippet:

$(document).on("change", ".kd1", function() {      $('table tr').each(function() {          $('.sum', this).text(              $('.kd1', this).get().reduce(function(sum, elem) {                 return sum + +$(elem).val();              }, 0)          );      });  });
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>  <table>  	<tr>  		<td><input type="text" class="kd1"></td>  		<td><input type="text" class="kd1"></td>  		<td class="sum"></td>  	</tr>  	<tr>  		<td><input type="text" class="kd1"></td>  		<td><input type="text" class="kd1"></td>  		<td class="sum"></td>  	</tr>    	  </table>


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -