I'm trying to autofill textbox based on dropdown selection in laravel

时间:2018-12-03 12:54:29

标签: jquery ajax laravel-5 autocomplete

1. This is my dropdown selection field inside the form

<div class="form-group">
    <label>Class ID</label>
    <select name="info_id" id="info_id" class="form-control info_id" required="">
        <option value="">-select-</option>
        @foreach($aiclasses as $aiclass)
        <option value="{{$aiclass->id}}">{{$aiclass->id}}</option>
        @endforeach
    </select>
</div>
  1. This is the textbox i want to autofill from db

    Required Fees
  2. This is my ajax code

@push('js')
<script>
    $(".info_id").change(function() {
        var id = $(this).val();
        $.ajax({
            url: '/info/' + $(this).val(),
            type: 'get',
           data: {id : 'id'},
           success: function(data) {
               if (data.success === true) {
                   $("#info_id").value = data.info;
               } else {
                   alert('Cannot find info');
               }
           },
          error: function(jqXHR, textStatus, errorThrown) {}
       });
    });
</script>
@endpush
  1. This is my controller
public function getInfo($id)
{
    $fill = DB::table('aiclasses')->where('id', $id)->pluck('fee');
    return \Response::json(['success'=>true, 'info_area'=>$fill]);
}

1 个答案:

答案 0 :(得分:0)

只需更新将值放入文本框的jquery代码:

 $("#info_id").val(data.info_area);