如何使用ajax更新laravel中的单选按钮值?

时间:2018-08-06 04:38:24

标签: ajax laravel-5.4

我想获取单选按钮值并将其通过AJAX发送到LARAVEL。

我的AJAX正在运行,但是当前在每行中插入0,因此它没有从单选按钮中获取值。任何帮助将不胜感激。

Blade.php

<div class="radio">
    <label>
        <input type="radio" name="teacher_type" id="teacher_type" value="0">Always
    </label>
    <label>
        <input type="radio" name="teacher_type" id="teacher_type" value="1">Temporary
    </label>
</div>

Ajax

    $(document).on('click', '#edit_communicated_teachers', function () {
        $("#communicated-teachers-lists").hide();
        $("#communicated-teachers-forms").fadeIn();
        teacher_id = $(this).parent().parent().find('#eteacher_id').text();
        teacher_type = $(this).parent().parent().find('#eteacher_type').text();

        $("#teacher_id").val(teacher_id);
        $("#teacher_type").val(teacher_type);

        $("#save-communicated-teachers").text("Edit");
        $('button[id="save-communicated-teachers"]').attr('type', 'button');
        $("#save-communicated-teachers").prop('id', 'update-communicated-teachers');
        $("#update-communicated-teachers").attr('data-id', $(this).data('id'));
    });

    $(document).on('click', '#update-communicated-teachers', function () {
        id = $(this).data('id');
        teacher_id = $('#teacher_id').val();
        teacher_type = $('#teacher_type').val();

        $.ajax({
            url: '/admin/communicated-teachers/' + id,
            type: 'PUT',
            dataType: 'json',
            data: {
                teacher_id: teacher_id,
                teacher_type: teacher_type,
                id: id,
                _token: '{{ csrf_token() }}'
            },
            success: function(data) {
                $('#data_communicated_teachers').html('');
                load_communicated_teachers();
            },
            error: function(response) {
                console.log(response);
            },
        });
    });

控制器

public function update(Request $request, CommunicatedTeacher $communicatedTeacher)
{
    if($request->ajax()) {
        $communicatedTeacher = CommunicatedTeacher::find($request->id);
        $communicatedTeacher->teacher_id = $request->teacher_id;
        $communicatedTeacher->teacher_type = $request->teacher_type;
        $communicatedTeacher->save();
        return response()->json();
    }
}

0 个答案:

没有答案