Load value into input text after selecting value in select Codeigniter

0

I want to load a value into an input text after selecting a value in a select. I'm loading the normal select. I want to load the input text using javascript. In my model I make a comparison with the id. The data is in the same table.

Java Script:

<script>
    var base_url = '<? echo base_url() ?>';
    function busca_produtos(id_alug){
    $.post(base_url+"parcelas/get_parcelas", {
        id_alug : id_alug
    }, function(data){
        $('#parcelas').html(data);
    });
}</script>

My input select working normal:

        echo "<select name='aquiller' id='id' class='form-control input-sm' onchange='busca_produtos($(this).val())'>";

My Controller:

 public function get_parcelas(){
    $parcelas = $this->sindico->get_parcelas();
    $option = "<option value=''></option>";
    foreach($parcelas -> result() as $linha) {
        $option .= "<option value='$linha->id_alug'>$linha->id</option>";
    }
    echo $option;
}
    
asked by anonymous 17.07.2016 / 01:54

2 answers

0

I think your select should already be written completely on the page, as it is in the code, it seems that only in the change event that the select is mounted.

Follow example to search for the value based on the selected select

<select id="opcoes">
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
</select>

<input type="text" id="valor">

<script>
    $(document).ready(function() {
        $("#opcoes").change(function(event) {
            var id_alug = $(this).val();
            $.ajax({
                url: 'buscar_produto.php',
                type: 'POST',
                data: {id_alug : id_alug},
            })
            .done(function(valor) {
                $("#valor").val(valor);
            })
            .fail(function() {
                console.log("error");
            });
        });
    });
</script>
    
17.07.2016 / 02:58
0

@fernandoandrade takes a look at how it got because in the comments it gets messy.

<script>
    var base_url = '<?php echo base_url()?>';
    $(document).ready(function() {
        $("#opcoes").change(function(event) {
            var id_alug = $(this).val();
            $.ajax({
                    url: 'base_url+"parcelas/get_parcelas"',
                    type: 'POST',
                    data: {id_alug : id_alug},
                })
                .done(function(valor) {
                    $("#valor").val(valor);
                })
                .fail(function() {
                    console.log("error");
                });
        });
    });
</script>
    
17.07.2016 / 03:15