Codeigniter 3 ajax 使用 jquery 从另一个输入进行计算

因此,我们的想法是操纵Ajax提供的价值。此值应除以 20,结果应乘以#nilai_hari输入的值。这是我的代码


// trigger

<select class="form-control" id="id_anggota" name="id_anggota">

    <option>-Pilih Karyawan</option>

    <?php $no=1; foreach ($listAnggota as $l) {?>

        <option value="<?php echo $l->id_anggota; ?>">

            <?php echo $l->nama_lengkap; ?>

        </option>

        <?php $no++; }?>

</select>

// the effected

<select class="tunjangan_makan form-control total_tunjangan nominal" id="tunjangan_makan" name="tunjangan_makan"></select>

// another trigger

<input type="text" id="nilai_hari" name="nilai_hari" class="form-control">

这是脚本


$('#id_anggota').change(function() {

    var id = $(this).val();

    $.ajax({

        url: "<?php echo base_url();?>hrd/penggajian/get_subpenggajian",

        method: "POST",

        data: {

            id: id

        },

        async: false,

        dataType: 'json',

        success: function(data) {

            var html = '';

            var i;

            for (i = 0; i < data.length; i++) {

                if (data[i].flat_tunjangan == 1) {

                    html += '<option value="' + data[i].tunjangan_makan + '">' + number_format(data[i].tunjangan_makan, 0, '', '.') + '</option>';

                } else {

                    $('#tunjangan_makan', '#nilai_hari').keyup(function() {

                        var nilai_hari = $("#nilai_hari").val(),

                            makan = ((data[i].tunjangan_makan / 20) * nilai_hari)

                        html += '<option value="' + makan + '">' + number_format(makan, 0, '', '.') + '</option>';

                    })

                }

            }

            $('.tunjangan_makan').html(html);

        }

    });

});

因此,flat_tunjangan为 0 的数据的值必须根据另一个输入计算得出。提前致谢


慕码人8056858
浏览 62回答 1
1回答

繁星点点滴滴

你做错了。您应该调用其他输入 onkey 事件并再次调用相同的 ajax 请求,并在 $('#id_anggota').change() 中删除该 onkey 事件。$('#id_anggota').change(function(){&nbsp; &nbsp; &nbsp; &nbsp; var id=$(this).val();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // perform ajax and get data&nbsp; &nbsp; &nbsp; &nbsp; var data = [{'flat_tunjangan': '1', 'tunjangan_makan': '20'}, {'flat_tunjangan': '0', 'tunjangan_makan': '30'}];&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var html = '';&nbsp; &nbsp; &nbsp; &nbsp; var i;&nbsp; &nbsp; &nbsp; &nbsp; for(i=0; i<data.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data[i].flat_tunjangan == 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html += '<option value="'+data[i].tunjangan_makan+'">'+data[i].tunjangan_makan+'</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var nilai_hari&nbsp; = $("#nilai_hari").val(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; makan = ((data[i].tunjangan_makan/20)*nilai_hari)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (makan != '') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html += '<option value="'+makan+'">'+makan+'</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $('.tunjangan_makan').html(html);&nbsp;});&nbsp;&nbsp;$("#nilai_hari").keyup(function(){&nbsp; &nbsp; var nilai_hari = $(this).val();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // again do ajax reqeust and fetch data using selected tunjangan_makan id&nbsp; &nbsp; var id = $('#id_anggota').val();&nbsp; &nbsp; var data = [{'flat_tunjangan': '1', 'tunjangan_makan': '20'}, {'flat_tunjangan': '0', 'tunjangan_makan': '30'}];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var html = '';&nbsp; &nbsp; var i;&nbsp; &nbsp; for(i=0; i<data.length; i++){&nbsp; &nbsp; &nbsp; if(data[i].flat_tunjangan == 1){&nbsp; &nbsp; &nbsp; &nbsp; html += '<option value="'+data[i].tunjangan_makan+'">'+data[i].tunjangan_makan+'</option>';&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var nilai_hari&nbsp; = $("#nilai_hari").val(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; makan = ((data[i].tunjangan_makan/20)*nilai_hari)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (makan != '') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html += '<option value="'+makan+'">'+makan+'</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $('.tunjangan_makan').html(html);&nbsp;});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select class="form-control" id="id_anggota" name="id_anggota">&nbsp; &nbsp; <option value="1">Test 1</option>&nbsp; &nbsp; <option value="2">Test 2</option></select><select class="tunjangan_makan form-control total_tunjangan nominal" id="tunjangan_makan" name="tunjangan_makan"></select><input type="text" id="nilai_hari" name="nilai_hari" class="form-control">
打开App,查看更多内容
随时随地看视频慕课网APP