Reputation: 843
I have a jquery problem, I am fetching results in an array from my sql database.
The items are in a table tr td
. Each row holds a unique id.
but when i do the sum or doing onkeyup for one text box the all total change also.
I am uding an onKeyUp
event for Qty
.
eg.:
Qty = 3
Bedrag = 20
Totaal (expected) = 60
Here is a screenshot:
Is my jQuery code wrong?
jQuery(document).ready(function(){
var j = 0;
/* COMPUTATION new item */
jQuery('input[id=new_quan_'+j+']').keyup(function(){
j++;
var new_sum = parseInt($('input[id=new_quan_'+j+']').val(), 10);
new_sum *= parseInt($('input[id=new_amt_'+j+']').val(), 10);
jQuery('input[id=new_total_'+j+']').val(new_sum);
});
});
Here is the HTML and PHP code:
<table cellpadding="5" cellspacing="0" width="100%" id="computation_table">
<tbody>
<tr>
<th>Categories</th>
<th>Qty</th>
<th>Omschrijving</th>
<th>Bedrag</th>
<th>Totaal</th>
<th>BTW</th>
<th></th>
</tr>
<?
$quo_com = mysql_query( "SELECT * FROM jon_com_quo_computation WHERE user_code = '".$_GET['uc']."' and footnote = 'new' and active='1' " ) or die ( mysql_error() );
$get_btw = mysql_fetch_array( $quo_com );
if ( $get_btw['currency'] == 'euro' ) {
$msg_tot = '€';
} elseif ( $get_btw['currency'] == 'usd' ) {
$msg_tot = '$';
}
$sqlview = mysql_query( "SELECT * FROM jon_com_quo_computation WHERE user_code = '".$_GET['uc']."' and footnote = 'new' and active='1' " ) or die ( mysql_error() );
$j = 0;
while( $row = mysql_fetch_array( $sqlview ) ) {
?>
<tr>
<td>
<input type="hidden" name="id_<?=$j?>" value="<?=$row['q_id'];?>" />
<input type="hidden" name="counter" value="<?=$j?>" />
<input type="text" name="category_<?=$j?>" class="text_com" value="<?=$row['category'];?>" />
</td>
<td>
<input type="text" name="quantity_<?=$j?>" id="new_quan_<?=$j?>" class="text_com" value="<?=$row['quo_quantity'];?>" /> x
</td>
<td width="200">
<input type="text" name="quo_definition_<?=$j?>" class="input" value="<?=$row['quo_definition'];?>" />
</td>
<td>
<input type="text" name="quo_amt_<?=$j?>" id="new_amt_<?=$j?>" class="text_com" value="<?=$row['quo_amt'];?>" />
</td>
<td id="total">
<input type="text" name="quo_total_<?=$j?>" id="new_total_<?=$j?>" class="text_com" value="<?=$row['quo_total'];?>" />
</td>
<td>
<input type="text" name="quo_btw_<?=$j?>" class="text_com" value="<?=$row['quo_btw'];?>" />
</td>
</tr>
<?
$j++;
}
?>
</tbody>
</table>
</div>
<br />
<!-- END OF COMPUTATION UPDATE -->
<input type="submit" name="up_item_list" value="Werk de vorm" /> - <input type="submit" name="new_save_list" value="Opslaan nieuw item" /> - <a href="">Annuleren</a>
</form>
Upvotes: 3
Views: 2337
Reputation: 6007
Maybe you need to use $(this)
and this.id
like this:
jQuery(document).ready(function(){
$('input[type=text]').each(function(){
$(this).keyup(function(){
var j = this.id.split('_')[2];
var new_sum = parseInt($('input[id=new_quan_'+j+']').val(), 10);
new_sum *= parseInt($('input[id=new_amt_'+j+']').val(), 10);
jQuery('input[id=new_total_'+j+']').val(new_sum);
});
});
});
Upvotes: 0
Reputation: 12815
It is better to use classes to find corresponding elements instead of that id built with j
(even have no idea how it should work looking at your code)
jQuery('.new_quan').keyup(function(){
var new_sum = parseInt(jQuery(this).val(), 10);
new_sum *= parseInt(jQuery(this).closest('tr').find('.new_amt').val(), 10);
jQuery(this).closest('tr').find('.new_total').val(new_sum);
});
And update php code:
while( $row = mysql_fetch_array( $sqlview ) ) {
?>
<tr>
<td>
<input type="hidden" name="id_<?=$j?>" value="<?=$row['q_id'];?>" />
<input type="hidden" name="counter" value="<?=$j?>" />
<input type="text" name="category_<?=$j?>" class="text_com" value="<?=$row['category'];?>" />
</td>
<td>
<input type="text" name="quantity_<?=$j?>" id="new_quan_<?=$j?>" class="text_com new_quan" value="<?=$row['quo_quantity'];?>" /> x
</td>
<td width="200">
<input type="text" name="quo_definition_<?=$j?>" class="input" value="<?=$row['quo_definition'];?>" />
</td>
<td>
<input type="text" name="quo_amt_<?=$j?>" id="new_amt_<?=$j?>" class="text_com new_amt" value="<?=$row['quo_amt'];?>" />
</td>
<td id="total">
<input type="text" name="quo_total_<?=$j?>" id="new_total_<?=$j?>" class="text_com new_total" value="<?=$row['quo_total'];?>" />
</td>
<td>
<input type="text" name="quo_btw_<?=$j?>" class="text_com" value="<?=$row['quo_btw'];?>" />
</td>
</tr>
<?
$j++;
}
Here I have added few classes to your inputs so than it is possible to find those elements in even handler with find .find('.new_total')
and .find('.new_amt')
.closest("tr")
will find a tr which contains all inputs you need to update total. and find('...')
will search inside that tr only, updating only related input values only that way.
Upvotes: 1