【问题标题】:Laravel 7 - Update value of checkbox without form or buttonLaravel 7 - 更新没有表单或按钮的复选框的值
【发布时间】:2022-01-24 19:52:36
【问题描述】:

我的项目卡住了,请帮忙...

情况是这样的:在我的项目中,我必须指定一位老师来授予学生评分权限,因为一个小组可以有 2 位或更多位老师,但只有一位必须有评分权限,所以...我添加了数据库中我的表“教师”中的一列,名称为“等级”,类型为布尔值。在我看来,我添加了一个复选框...如何在不使用任何表单或按钮触发操作并激活将有权评分的老师的复选框的情况下更改复选框的值?并且这个值也应该在数据库中更新。

  • 这是我的看法...

    <!--teachers-->
    <table class=" table table-sm table-striped table-bordered ">
        <thead class="text-white bg-primary">
            <tr>
                <th>@lang('show.Identifier')</th>
                <th>@lang('show.email')</th>
                <th>@lang('show.grade')</th>
                <th>@lang('show.delete')</th>
            </tr>
        </thead>
        
        <tbody class="small text-center">
            @foreach ($group->teachers as $teacher)
                <tr>
                    <td >{{$teacher->Identifier}} </td>
                    <td >{{$teacher->email}} </td>
                    <td>
                        <input id="active" {{$teacher->grade == 1 ?'checked':''}} type="checkbox" name="grade" value="1">
                    </td>
                    <td>
                        <form method="post" action="{{url('Groups/Disenroll/'.$group->idGroup.'?idTeacher='.$teacher->idTeacher)}}">
                        {{csrf_field()}}
                            <button type="submit"  class="btn btn-sm btn-outline-danger"  > <i class="material-icons small" >call_split</i></button>
                        </form>
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>

这是一张桌子的图片,如果有帮助的话......

【问题讨论】:

    标签: javascript php jquery laravel checkbox


    【解决方案1】:

    我认为您可以为此使用 JQuery 之类的东西。

    您可以先在您的复选框中添加一个 toggle-class

    <input class="active toggle-class" {{$teacher->grade == 1 ?'checked':''}} type="checkbox" name="grade">
    

    其次,确保你有办法跟踪谁在做成绩。

    在您的复选框上添加 data-id="{{ $teacher->Identifier}}"

    <input data-id="{{ $teacher->Identifier}}" class="active toggle-class" {{$teacher->grade == 1 ?'checked':''}} type="checkbox" name="grade">
    

    在您的 web.php 中添加一条新路由

    Route::get('/path/to/updateGrader/status', [App\Http\Controllers\MyController::class, 'updateGraderStatus']);
    

    将以下代码添加到您的复选框所在的位置

    $('.toggle-class').on('change',function(){
        let status = $(this).prop('checked') == true ? true : false;
        let grader_id = $(this).data('id');
        $.ajax({
           type:'GET',
           dataType:'json',
           url:'/path/to/updateGrader/status', // add your earlier created route here
           data:{'grade': status, 'grader_id': grader_id},
           success: function(data){
            console.log('success');
         }
       });
    });
    

    在您的控制器中,MyController 在我们的例子中创建一个新函数

    public function updateGraderStatus(Request $request)
    {
           $grader = MyModel::find($request->grader_id);
           $grader->grade = $request->grade;
           $grader->save(); 
    }
    

    如果这对您有帮助,请告诉我。我没有对其进行测试,但我认为您会这样做。

    【讨论】:

    • 它给了我这个错误 500(内部服务器错误)。控制台上的完整响应是这样的:jquery.js:9664 GET http://localhost/Servescol2.0.2/public/Groups/StatusGrade?grade=0&amp;id=hframosb 500 (Internal Server Error)
    • 这种类型的错误大多与控制器函数或 web.php 有关。由于您使用的是 Laravel 7,您是否按照 Laravel 7 编写了路由定义?例如。 Route::get('/path/to/updateGrader/status', 'MyController@updateGraderStatus');?
    • 是的,这是我的路线:Route::get('StatusGrade', 'GroupController@UpdateStatusGrade')->middleware('auth');
    • 点击这个回复看看它在抱怨什么。如果我没记错的话,我认为这个链接应该是可点击的:jquery.js:9664 GET localhost/Servescol2.0.2/public/Groups/… 500 (Internal Server Error) on your console 我的意思是
    • 我认为是这个错误:ErrorException Trying to get property 'identifier' of non-object (View: C:\xampp\htdocs\Servescol2.0.2\resources\views\group\show.blade. php)。但这没有意义,因为这个属性工作正常。观点相同,但之前没有给我带来问题。
    猜你喜欢
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2019-06-10
    • 2015-11-28
    相关资源
    最近更新 更多