Javascript 当收音机改变时,重新计算每行的总和
你好 我有一个动态表格,当您按下X按钮时,您可以在其中添加和删除表格,每行有一个当前数量字段和调整数量字段,还有一个新数量字段,当您在调整数量字段上输入数字时,该字段将派生,它的所有工作正常,但我想添加一个功能,将改变每个总数量字段的基础上选中的电台框以上的表。与单击IN(单选按钮)类似,操作将是ADD,如果单击OUT(单选按钮),操作将是减号,并且所有行都应受到单选按钮的每个“onchange”事件的影响。谁能帮帮我:) HTML:Javascript 当收音机改变时,重新计算每行的总和,javascript,jquery,html,Javascript,Jquery,Html,你好 我有一个动态表格,当您按下X按钮时,您可以在其中添加和删除表格,每行有一个当前数量字段和调整数量字段,还有一个新数量字段,当您在调整数量字段上输入数字时,该字段将派生,它的所有工作正常,但我想添加一个功能,将改变每个总数量字段的基础上选中的电台框以上的表。与单击IN(单选按钮)类似,操作将是ADD,如果单击OUT(单选按钮),操作将是减号,并且所有行都应受到单选按钮的每个“onchange”事件的影响。谁能帮帮我:) HTML: 部门 @foreach($部门作为$部门) {{$dep
部门
@foreach($部门作为$部门)
{{$department->department}
@endforeach
调整类型
库存
清点
评论
项目
现存量
调整数量
新数量
当前成本
调整成本
成本差异
到期日期
去除
项目1
20
10
30
10.40
12
.60
11/21/1016
X
项目31
230
16
246
31.40
20
-11.40
11/21/1019
X
项目6
12
19
31
22.40
30
7.60
11/21/1016
X
JS:
var$this=$(文档);
$this.on(“更改”,“输入[name=“optradio”]”,函数(){
var rowCount=$('#adjustmentTable tr')。长度-1;
});
您需要为这些单选按钮上的“更改”事件创建事件处理程序。然后,这些事件处理程序将在表的所有行中循环,并对每一行应用AutoCalculatedDifference函数。大概是这样的:
$("#radioIn, #radioOut").change(function() {
// The user clicked a radio button. Now loop through all the
// rows in the table. NOTE: You should be more specific with
// this jQuery selector to refer to the exact ID of the table.
$("table tr").each(function() {
autoCalculateDifferenceOnRow(this);
});
});
如果这样做,则需要稍微重构AutoCalculatedDifference函数,以便能够处理表示行的传入参数,而不是始终使用“this”。比如说,
function autoCalculateDifferenceOnRow(currentRow) {
if(document.getElementById('radioIn').checked) {
var type = 1;
}else if(document.getElementById('radioOut').checked) {
var type = 0;
}
var $adjQty = $(currentRow).find('.adjQty');
var $newCost = $(currentRow).find('.newCost');
var $onHandQty = $(currentRow).find('p.onHandQty');
var $qtyDiff = $(currentRow).find('p.qtydiff');
var $currentCost = $(currentRow).find('p.currentCost');
var $costDiff = $(currentRow).find('p.costDiff');
// Update Quantity
var onHandQty = parseInt($onHandQty.text(),10);
if(type == 1)
{
var difference = onHandQty + parseInt($adjQty.val());
}
else if(type==0)
{
var difference = onHandQty - parseInt($adjQty.val());
}
$qtyDiff.text(difference);
// Update Cost
var currentCost = $currentCost.text();
var difference = $newCost.val() - currentCost;
$costDiff.text(difference);
}
我还没有测试上述代码,但希望它能帮助您朝着正确的方向前进。哪部分不起作用?在jsfiddle.net或其他代码共享站点上进行演示会有所帮助。当我们可以与codeHello@charlietfl交互时,帮助会简单得多。谢谢您的光临!当我单击单选按钮时,我希望每个新的输入行都将根据我选择的选项重新计算,如果是库存输入(操作应为加法),如果是库存输出(应为减法),但我不知道如何实现此功能,将html放入演示…似乎你的
如果应该做你想做的事情只要看一下codeHi@charlietfl抱歉,我无法在JSFIDLE上运行,但我在上面添加了代码。你至少可以提供源html而不是服务器模板吗?很容易从browserIt复制它工作得很好,先生@Rajeev Goel,非常感谢你的帮助。我甚至没有修改你的代码,你真是个天才,先生。非常感谢:)
$("#radioIn, #radioOut").change(function() {
// The user clicked a radio button. Now loop through all the
// rows in the table. NOTE: You should be more specific with
// this jQuery selector to refer to the exact ID of the table.
$("table tr").each(function() {
autoCalculateDifferenceOnRow(this);
});
});
function autoCalculateDifferenceOnRow(currentRow) {
if(document.getElementById('radioIn').checked) {
var type = 1;
}else if(document.getElementById('radioOut').checked) {
var type = 0;
}
var $adjQty = $(currentRow).find('.adjQty');
var $newCost = $(currentRow).find('.newCost');
var $onHandQty = $(currentRow).find('p.onHandQty');
var $qtyDiff = $(currentRow).find('p.qtydiff');
var $currentCost = $(currentRow).find('p.currentCost');
var $costDiff = $(currentRow).find('p.costDiff');
// Update Quantity
var onHandQty = parseInt($onHandQty.text(),10);
if(type == 1)
{
var difference = onHandQty + parseInt($adjQty.val());
}
else if(type==0)
{
var difference = onHandQty - parseInt($adjQty.val());
}
$qtyDiff.text(difference);
// Update Cost
var currentCost = $currentCost.text();
var difference = $newCost.val() - currentCost;
$costDiff.text(difference);
}