我的单选按钮无法使用 ajax jquery 在 laravel 8 中自动保存数据

my radio button can not auto save data with ajax jquery in laravel 8

有人可以帮助我编写代码吗?我的单选按钮无法使用 ajax jquery 在 laravel 8 中自动保存数据,而且我没有收到错误消息留言

我的控制器:Q_AnswersController

public function storeAns(Request $request)
    {
        
        $model = new Q_Answer;
        $model->user_id = Auth::id();
        $model->topic_id = $request->topic_id;
        $model->question_id = $request->question_id;
        $model->user_answer = $request->radio_ans;
        $model->answer = $request->answer;
        $topik = $request->topic_id;
        
        $model->save();
               
        return redirect('qanswers/'. $topik);
    }

这是我的路线:

Route::post('/storeAns',[Q_AnswersController::class, 'storeAns'])->name('storeAns');

这是我的 blade :

@foreach ($datas as $key=>$value)
                    <p>{{ $value->question }}</p>
                    <p><input type="radio" id="radio1" name="radio_ans" value="{{ $value->a }}"><label>&nbsp;&nbsp;{{ $value->a }}</label></p>
                      <p><input type="radio" id="radio2" name="radio_ans" value="{{ $value->b }}"><label>&nbsp;&nbsp;{{ $value->b }}</label></p>
                        <p><input type="radio" id="radio3" name="radio_ans" value="{{ $value->c }}"><label>&nbsp;&nbsp;{{ $value->c }}</label></p>
                        <p><input type="radio" id="radio4" name="radio_ans" value="{{ $value->d }}"><label>&nbsp;&nbsp;{{ $value->d }}</label></p>
                        <p><input type="radio" id="radio5" name="radio_ans" value="{{ $value->e }}"><label>&nbsp;&nbsp;{{ $value->e }}</label></p>
                        <p><input type="radio" id="radio6" name="radio_ans" value="{{ $value->f }}"><label>&nbsp;&nbsp;{{ $value->f }}</label></p>
                        <input type="hidden" name="topic_id" class="form-control" readonly value="{{ $value->topic_id }}">
                        <input type="hidden" name="answer" class="form-control" readonly value="{{ $value->answer }}">
                        <input type="hidden" name="id" class="form-control" readonly value="{{ $value->id }}">
                        <input type="hidden" name="question_id" class="form-control" readonly value="{{ $datas->currentPage() }}">
                        <input type="hidden" name="user_id" class="form-control" hidden readonly value=" {{ Auth::id(); }} " >
                    @endforeach

这是我的脚本:

$(document).ready(function() {
        $('input[type="radio"]').click(function() {
            var user_id = $("#user_id").val();
            var topic_id = $("#topic_id").val();
            var question_id = $("answer").val();
            var answer = $("answer").val();
            var user_answer = $("radio_ans").val();
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url: "{{ url('qanswers.storeAns') }}",
                method: "POST",
                data:{
                    user_id:user_id,
                    topic_id:topic_id,
                    question_id:question_id,
                    answer:answer,
                    user_answer:user_answer
                } ,
                success: function(data) {
                    $('#alert').text('Data Inserted Successfully..');
                }
            });
        });
    });

非常感谢各位大侠帮忙,谢谢

您正在 ajax 请求和后端发送 user_answer 密钥,读取 $request->radio_ans 而不是 $request->user_answer。所以改变这一行:

 $model->user_answer = $request->radio_ans;

 $model->user_answer = $request->user_answer;

您使用 jquery 中的单选按钮名称获取单选按钮值 但是这个语法给出的值是未定义的

var user_answer = $("radio_ans").val();

你可以试试这个 -

var user_answer = $("input[name=radio_ans]:checked").val();