Javascript 如何在不影响上一行的情况下更改表的动态行中的值?
在动态表中,当我输入行的借方列时,贷方列应显示为“0”。 当我输入行的贷方列和借方列时,显示“0”。 我的问题是,如果我在借方列中输入,贷方列显示“0”。如果我在贷方列的下一行中输入,上一行借方列也显示“0”值。只有现有行仅显示值“0”。我的代码也影响上一行Javascript 如何在不影响上一行的情况下更改表的动态行中的值?,javascript,Javascript,在动态表中,当我输入行的借方列时,贷方列应显示为“0”。 当我输入行的贷方列和借方列时,显示“0”。 我的问题是,如果我在借方列中输入,贷方列显示“0”。如果我在贷方列的下一行中输入,上一行借方列也显示“0”值。只有现有行仅显示值“0”。我的代码也影响上一行 <script> $(document).ready(function(){ $('.Debit').on('change input',function() { var
<script>
$(document).ready(function(){
$('.Debit').on('change input',function() {
var amount = 0;
var hh2 = 0;
$('.tb3 > tbody > tr').each(function() {
var pamt = $(this).find('.Debit').val();
$(this).find('.Credit').val(0);
hh2 += parseFloat(pamt);
});
$('#TotD').val(hh2);
});
$('.Credit').on('change input',function() {
var amount = 0;
var hh2 = 0;
$('.tb3 > tbody > tr').each(function() {
$(this).find('.Debit').val(0);
var pamt = $(this).find('.Credit').val();
hh2 += parseFloat(pamt);
});
$('#TotC').val(hh2);
});
});
</script>
<div class="col-xs-12">
<div class="table-responsive pre-scrollable">
<table class="table table-bordered table-striped table-xxs tb3" id="tb3">
<thead>
<tr>
<th></th>
<th>Account Name</th>
<th>Debit</th>
<th>Credit</th>
<th>Particulars</th>
<th>Cost Center</th>
<th></th>
</tr>
</thead>
<tbody>
<tr >
<td><a href='javascript:void(0);' class='remove3'><span class='glyphicon glyphicon-remove'></span></a></td>
<td><select style="width:120px" class="form-control" name="name[]" id="name">
<option></option>
<?php foreach ($PName as $row ): ?>
<option value="<?=$row['name']?>"><?=$row['name']?></option>
<?php endforeach ?>
</select></td>
<td ><input style="width:80px" type="text" class="form-control input-xs Debit" name = "Debit[]" > </td>
<td><input style="width:80px" type="text" class="form-control input-xs Credit" name="Credit[]"></td>
<td><input style="width:100px" type="text" id="" class="form-control input-xs" value="" name="Parti[]"></td>
<td><input style="width:80px" type="text" id="Cost" class="form-control input-xs" value="" name="Cost[]"></td>
<td><a href="javascript:void(0);" style="font-size:18px;" id="addMore3" title="Add More Person"><span class="glyphicon glyphicon-plus"></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-xs-12 clearfix">
<div class="col-xs-6">
<div class="form-group "><br>
<label class="col-md-4 control-label">Total:</label>
<div class="col-md-4">
<input type="text" placeholder="Your Amount" id="TotD" class="form-control grandto" name="TotD" value="0" >
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group "><br>
<div class="col-md-4">
<input type="text" placeholder="Your Amount" id="TotC" class="form-control grandto" name="TotC" value="0" >
</div>
</div>
</div>
</div>
$(文档).ready(函数(){
$('.Debit')。在('change input',function()上{
风险价值金额=0;
var hh2=0;
$('.tb3>tbody>tr')。每个(函数(){
var pamt=$(this.find('.Debit').val();
$(this.find('.Credit').val(0);
hh2+=pamt;
});
$('TotD').val(hh2);
});
$('.Credit')。在('change input',function()上{
风险价值金额=0;
var hh2=0;
$('.tb3>tbody>tr')。每个(函数(){
$(this.find('.Debit').val(0);
var pamt=$(this.find('.Credit').val();
hh2+=pamt;
});
$('TotC').val(hh2);
});
});
帐户名
借记
信用
详情
成本中心
总数:
你有小提琴吗
我还看到您正在使用类来定位元素。这将影响具有该类的所有元素
还要记住,find()搜索在所有元素上执行代码的元素的所有子代,如下所示:
var pamt = $(this).find('.Credit').val();
这是因为您通过循环
将所有贷方或借方值设置为0!因此,将设置值0移到每个之外
$('.Debit').on('change input',function() {
$(this).parent().parent().find('.Credit').val(0); // move from each loop
...remaining code
$('.Credit').on('change input',function() {
$(this).parent().parent().find('.Debit').val(0); // move from each loop
...remaining code
你可以试试类似的东西-
$('.Debit').on('change input', function() {
var debit = 0;
var credit = 0;
$(this).closest('tr').find('.Credit').val(0);
$('.tb3 > tbody > tr').each(function() {
debit += parseFloat($(this).find('.Debit').val());
credit += parseFloat($(this).find('.Credit').val());
});
$('#TotD').val(debit);
$('#TotC').val(credit);
});
$('.Credit').on('change input', function() {
var debit = 0;
var credit = 0;
$(this).closest('tr').find('.Debit').val(0);
$('.tb3 > tbody > tr').each(function() {
debit += parseFloat($(this).find('.Debit').val());
credit += parseFloat($(this).find('.Credit').val());
});
$('#TotD').val(debit);
$('#TotC').val(credit);
});
注意:由于当您在借方字段中输入该行的贷方值变为0时,您的包括借方和贷方在内的总计算值也应不仅反映一个(贷方或借方)。我还删除了amount varibale,因为它没有被使用。什么是TotC,TotD-这个ID是那个贷记和借记单元格的吗?如果您共享的html代码不是行的id,这将非常有用,即计算所有行的贷记和借记的总和,并显示在totC和totd文本框中。您可以为此添加html代码,以便我们能够轻松理解问题吗?我编辑了代码siri无法understand@Cre3z是对的,,find函数将返回该类的所有元素。在onchange事件中,这个
应该指的是正在更改的元素,因此我建议使用获取其父元素(tr),然后使用它来查找另一个借方/贷方框。因为只有我在使用我必须更改的内容,所以我不能understand@Tom删除each()$(this.find('.Credit').val(0)中的此行;好的,那么我如何在输入借方列和借方列时将贷方列显示为零versa@Tom您是否删除了each()$(this.find('.Credit').val(0)中的这一行,那么David所做的上述更改将起作用so@IllllIllSanthoshKumarIllllI对!@Tom,所以我将注释移出每个范围中的设置代码