[英]how to count the values of the values of the dynamically added table rows using jquery
hi i have a form which dynamically adds a table row from an external file when a user clicks on a add button and i even want to place a calculation on one of my fields which is qty i want to calculate the value of the qty and show the total so far i have written a code to calculate but it seem not to work can anybody help me 嗨,我有一个窗体,当用户单击添加按钮时,它会从外部文件动态添加表格行,我什至要对我的其中一个字段qty进行计算,我想计算qty的值并显示到目前为止,我已经写了一个代码来计算,但似乎没有用,有人可以帮我吗
here is my script 这是我的剧本
$(document).ready(function(){
$('#detail').on('keyup', '.qty', calculateRow());
$('#addnew').click(function(){
var ctr = $('#items').val();
ctr++;
$.post('job_srch.php', {ctr : ctr}, function(data) {
$(data).appendTo('#detail');
$('#items').val(ctr);
});
});
function calculateRow() {
alert("GAYA");
console.log("2");
var add = 0;
$(".qty").each(function() {
add += parseFloat(this.value);
if (isNaN(add)) {
$('#total').val("0");
} else {
$('#total').val(add.toFixed(2));
}
});
}
});
here is my php 这是我的PHP
<?php
session_start();
require("includes/dbconnect.php");
include ('includes/function.php');
$zdb = $_SESSION["zdbyear"];
mysql_select_db($zdb);
if ($_REQUEST["ctr"]){
$ctr = $_REQUEST["ctr"];
//echo '<link rel="stylesheet" href="css/chosen.css" />';
echo '<tr>
<td align="center"><input type="text" size="6" maxlength="6" maxlength="6" name="srno_'.$ctr.'" class="form-input-oth"/></td>
<td align="center"><select data-placeholder="Party" style="width:300px;" name="item_'.$ctr.'" class="chzn-select-deselect" >
<option value=""></option>';
$res = mysql_query("SELECT * FROM `items`") or die(mysql_error());
while ($row = mysql_fetch_array($res)) {
echo "<option value='$row[accode]'>$row[name]</option>";
}
echo '</select></td>';
echo '<td align="center"><input type="text" id="qty" size="6" maxlength="9" maxlength="6" name="qty_'.$ctr.'" class="qty form-input-amt"/></td>
</tr>';
}
else{
echo "ERROR";
}
?>
here is my table 这是我的桌子
<table id="detail" border="1px" width="30%">
<tr>
<td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Sr No.</font><span></span></label></td>
<td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">item</font><span></span></label></td>
<td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Quantity</font><span></span></label></td>
</tr>
</table>
Tthe first and most obvious error I found was that the command "add += parseFloat(this.value);" 我发现的第一个也是最明显的错误是命令“ add + = parseFloat(this.value);”。 does not work. 不起作用。
The correct functions would be the following: 正确的功能如下:
function calculateRow() {
alert("GAYA");
console.log("2");
var add = 0;
$(".qty").each(function() {
add += parseFloat($(this).val()); // <-- changed from this.value
if (isNaN(add)) {
$('#total').val("0");
} else {
$('#total').val(add.toFixed(2));
}
});
}
You also have to change the following row from 您还必须更改以下行
$('#detail').on('keyup', '.qty', calculateRow());
to 至
$('#detail').on('keyup', '.qty', calculateRow);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.