使用codeigniter中的ajax更改输入值

时间:2016-09-15 09:44:11

标签: ajax codeigniter

我想根据codeigniter中的组合框中的选择更改输入值。我试着编码但是没有什么可以显示的。 这是我控制器中的代码.....

function fill_info()
{
    // retrieve the group and add to the data array
    $group_id = $this->input->post('group_id');
    $data = "0.00";
    if($group_id)
    {
        $this->load-model('Base_amount_setting_model');
        $baseamount = $this->Base_amount_setting_model->getbaseamount($group_id);
        $data .= $baseamount;
        echo $data;
    }
    else 
    {
        echo $data;
    }
}

在我的Base_amount_setting_model中有这种方法......

function getbaseamount($group_id)
{
    $this->db->where('group_id',$group_id);
    $baseamount = $this->db->get('base_amount_setting')->row()->amount;
    if($baseamount -> num_rows() == 1)
    {
        return $baseamount->result();
    }
}

在我看来,ajax看起来像这样......

<script>
    $(document).ready(function() 
    {
        $("#group_id").change(function() 
        {
            var group_id = $("#group_id").val();
            $.ajax({
                type : "POST",
                url  : "<?php echo base_url('payment/fill_info'); ?>",
                data : "group_id=" + group_id,
                success: function(data) 
                {
                    $("#base_amount").html(data);
                }
            });
        });
    });
</script>

最后我的表格是这样的......

<div class="control-group">
    <label class="control-label" for="select01">Group Id </label>
    <div class="controls">
        <select class="chzn-select" name="group_id" id="group_id" placeholder="Group Id" value="<?php echo $group_id; ?>">
            <option></option>
            <?php
            if (count($groups)) {
                foreach ($groups as $list) {
                    echo "<option value='". $list['group_id'] . "'>" . $list['group_name'] . "</option>";
                }
            }
            ?>
        </select>
        <label for="int" class="err"><?php echo form_error('group_id') ?></label>
    </div>
    <input class="input-xlarge disabled" id="base_amount" name="base_amount" type="text" placeholder="Base Amount" disabled="">
    <input class="input-xlarge disabled" id="total_members" type="text" placeholder="Total Members" disabled="">
</div>

谢谢!

3 个答案:

答案 0 :(得分:2)

更改此$("#base_amount").html(data);$("#base_amount").val(data);

答案 1 :(得分:1)

实际上,如果你在data对象的ajax命中之后获得了你的价值 然后才改变这个:

$("#base_amount").html(data);

$("#base_amount").val(data);

实际上.html重复html而不是更改值。

答案 2 :(得分:0)

我非常好奇 - 但我认为你的模特不会回报任何东西 尝试以下

function getbaseamount($group_id)
{
    $query = $this->db
        ->where('group_id',$group_id)
        ->get('base_amount_setting');

    if ($query->num_rows() == 1)
    {
        $obj = $query->row();
        return $obj->amount;
    }
}

和其他人一样建议

将脚本代码更改为

$("#base_amount").val(data);

并且您的控制器功能应该看起来像

function fill_info()
{
    // retrieve the group and add to the data array
    $group_id = $this->input->post('group_id');
    $data = "0.00";
    if($group_id)
    {
        $this->load-model('Base_amount_setting_model');
        $baseamount = $this->Base_amount_setting_model->getbaseamount($group_id);
        echo $baseamount;
    }
    else 
    {
        echo $data;
    }
}