javascript-如何在不影响上一个的情况下更改表的动态行中的值?

前端之家收集整理的这篇文章主要介绍了javascript-如何在不影响上一个的情况下更改表的动态行中的值? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在动态表中,当我输入行的借方列时,贷方列应显示为“ 0”.
当我输入行的贷方列,借方列显示为“ 0”.
 我的问题是,如果我在借方列中输入,贷方列显示为“ 0”.如果我在贷方列的下一行中输入,则上一行借方列也显示为“ 0”.仅现有行仅显示值“ 0“.我的代码也影响上一行.

<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>
最佳答案
这是因为每个循环都将所有贷方或借方值都设置为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
原文链接:https://www.f2er.com/js/531120.html

猜你在找的JavaScript相关文章