我有一个 jquery 问题,我正在从我的 sql 数据库中获取一个数组中的结果。
这些项目在 table tr td
中。每行都有一个唯一的 ID。
但是当我对一个文本框进行求和或 onkeyup 时,所有的总和也会发生变化。
我正在为 Qty
使用 onKeyUp
事件。
例如:
Qty = 3
Bedrag = 20
Totaal (expected) = 60
截图如下:
我的 jQuery 代码有错吗?
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);
});
});
这是 HTML 和 PHP 代码:
<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>
最佳答案
最好使用类来查找相应的元素,而不是使用 j
构建的 id(即使看你的代码也不知道它应该如何工作)
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);
});
并更新 php 代码:
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++;
}
我在这里为您的输入添加了几个类,这样就可以使用 find .find('.new_total')
和 .find('. new_amt')
.closest("tr")
将找到一个 tr,其中包含您需要更新总计的所有输入。并且 find('...')
将仅在该 tr 内搜索,仅以这种方式仅更新相关的输入值。
关于javascript - 带表的jQuery如何计算用户输入值的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13933866/