如何从输入字段模态引导中获取值并使用jQuery在同一模态中将值显示给另一个输入字段?

我有代码HTML:

<div class="modal fade" id="modalTambahDataTransaksiZakat">
<div class="vertical-alignment-helper">
    <div class="modal-dialog vertical-align-center">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">TAMBAH DATA TRANSAKSI ZAKAT</h4>
            </div>
            <div class="modal-body">
                <form role="form" action="" method="post">
                    <div class="box-body">
                         <div class="form-group">
                            <label>Gaji Muzakki</label>
                            <input type="number" class="form-control" id="gaji_muzakki " 
                        name="gajiMuzakki">
                        </div>
                        <div class="form-group">
                            <label>Nominal Pembayaran</label>
                            <input type="number" class="form-control" id="nominal_pembayaran" 
                             name="nominalPembayaran">
                        </div>
                    </div>
            </div>
            </form>

我在输入字段“ gaji muzakki”中输入值并用jquery将值显示给输入字段“ nominal pembayaran”后,如何从输入字段“ gaji muzakki”中获取值?

评论
  • XoXo
    XoXo 回复

    如果我对您的理解正确,那么它应该很简单:

    $(document).on('input', 'input[name="gajiMuzakki"]', function() {
        // Set the value into $value.
        let $value = $(this).val();
    
        // Populate 'nominalPembayaran' with this value.
        $(document).find('input[name="nominalPembayaran"]').val($value);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <input type="text" name="gajiMuzakki" placeholder="Gaji Muzakki">
    <input type="text" name="nominalPembayaran" placeholder="Nominal Pembayaran">
  • ysunt
    ysunt 回复

    这可以解决问题

    $('#gaji_muzakki').change(function(){
      $('#nominal_pembayaran').val($(this).val())
     })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="text" id="gaji_muzakki">
     
     <input type="text" id="nominal_pembayaran">