简体   繁体   中英

Populate text box from a dropdown using codeigniter

this is my table in the database

allowance

id |      type         |    allowance_value   | 
1  |  Meal Allowance   |        100.00        |
2  |  Travel Allowance |        150.00        |

payroll

payroll_id | employee_id | date_generated | allowance_id | allowanceValue
     1     |       2     |  2016-01-15    |     1        |    100.00
     2     |       3     |  2016-01-15    |     1        |    100.00

when selecting the type from the allowance table, i'm using a drop down menu and here is my code.

<?php echo form_dropdown('emp_allowance', $dropdown, (isset($_POST['emp_allowance']) ? $_POST['emp_allowance'] : ''), 'class="form-control" id="emp_allowance"'); ?>  

and I have a textbox that will populate if i select the type of allowance

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

How do I populate the text box when I select "Meal Allowance", the text box value must be "100.00" and if I add another type of allowance the text box value must vary according to the dropdown selected.

You can create a jquery snippet:

<script>
$('#emp_allowance').on('change', function() {
    var selected = $( "#emp_allowance" ).val();
    $('input[name="total_allowance"]').val(selected);
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM