weixin_33719619 2016-06-08 07:13 采纳率: 0%
浏览 33

jQuery多个onChange

view screenshot web

In my case, each chose "PILIH PAKET JENIS HARGA" then every row in the column "Harga" will be filled equal in value

whereas I want is every option on "PILIH PAKET JENIS HARGA" to change the line in the "Harga" in addition to also change each corresponding to the result of multiplying the column "Qty".

here my code:

<script type="text/javascript">

    $(document).ready(function(){

    // Tambah Row tabel
        $('#btn_paket_show').click(function(){          
            $('.tabel_paket tbody').append('<tr class="baris"><td style="width:50px; text-align:center; vertical-align:middle;"><input type="hidden" name="msg[]"><button name="btn_paket_hide[]" type="button" class="btn btn-danger btn-xs btn_paket_hide"><span class="glyphicon glyphicon-trash"></span></button></td><td><input type="text" class="form-control jml_paket" id="jml_paket" name="jml_paket[]" value"" placeholder="0"></td><td><select class="form-control nm_paket_hrg" id="nm_paket_hrg" name="nm_paket_hrg[]" ><option>PILIH JENIS PAKET HARGA</OPTION><?php foreach($list_paket as $s){?><option value="<?php echo ($s->id_item_paket_hrg."~".$s->nm_paket_hrg."~".$s->hrg);?>"><?php echo $s->hrg;?> @ <?php echo $s->nm_paket_hrg;?></option><?php }?></select></td><td><input class="form-control harga" id="harga[]" name="harga[]" type="text"></td></tr>');
        });

        $(document).on('change', '.nm_paket_hrg', function(){           
            var a_a = $(this).val();
            var a = a_a.split("~");
            var b = $('.jml_paket').val();
            var c = a[2]*b;

            $('.harga').val(c);
        });
</script>


<!-- TAMBAH PAKET -->
<button type="button" class="btn btn-info" id="btn_paket_show"><span class="glyphicon glyphicon-plus"> TAMBAH PAKET</button>
<table class="table table-striped table-bordered table-hover tabel_paket">
    <thead class="thead-2">
        <tr class="tr-2">
            <th class="th-2" style="width:50px; text-align:center;"><span class="glyphicon glyphicon-trash"></span></th>
            <th class="th-2" style="width:150px">QTY (Kg/Pcs)</th>
            <th class="th-2" style="width:300px">Nama Paket Harga</th>
            <th class="th-2" style="width:200px">Harga (Rp)</th>
        </tr>
    </thead>
    <tbody class="tbody-2">
    </tbody>
    <thead class="thead-2">
        <tr class="tr-2">
            <th class="th-2" colspan="3" style="text-align:right; vertical-align:middle">Total Harga (Rp)</th>
            <th class="th-2"><input class="form-control" id="total_harga" name="total_harga" type="text" placeholder="0"></th>
        </tr>
    </thead>
</table>

any soulution?

  • 写回答

2条回答 默认 最新

  • weixin_33717298 2016-06-08 07:30
    关注

    If you only want to set the value of the .harga element in the same row as the .nm_paket_hrg element, you need to limit this line:

    $('.harga').val(c);
    

    ...since as-is, it matches all .harga elements and so sets the value of all of them.

    Since this in the handler references the .nm_paket_hrg that changed, you can use it to navigate to the row and from there to the .harga:

    $(this).closest("tr").find(".harga").val(c);
    

    You probably want to do that for this line as well:

    var b = $('.jml_paket').val();
    

    ...since that will read the value of the first element matching .jml_paket.

    So:

    $(document).on('change', '.nm_paket_hrg', function() {
        var $this = $(this);                    // Reuse this
        var $row = $this.closest("tr");         // Find the row
        var a_a = $this.val();
        var a = a_a.split("~");
        var b = $row.find('.jml_paket').val();  // Just the one on this row
        var c = a[2] * b;
    
        $row.find(".harga").val(c);             // Just the one on this row
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探