简体   繁体   English

Laravel 7 - 取消选中 AJAX 中的复选框

[英]Laravel 7 - Uncheck the checkbox in AJAX

I'm not really good at ajax.我不太擅长 ajax。 I want to uncheck the checkbox for the 'active' users.我想取消选中“活动”用户的复选框。

  • I have this in my LoginController我的 LoginController 中有这个
   

     public function login()
            {
                $credentials = $this->validate(request(), [
                    'email' => 'email|required|string',
                    'password' => 'required|string',
                ]);
        
    
            if (Auth::attempt($credentials)) { //auth attemptdevuelve verdadero o falso en caso de que las credenciales correspondan o no
                //Inician cambios RDAN
                $user = Auth::user();
              if($user->active)  // checking your user is active or not
            {
                if ($user->hasRole('admin')) {
                    return redirect('main');
                } else if ($user->hasRole('externo')) {
                    return redirect('es/user_form');
                } else if ($user->hasRole('profesor')) {
                    return redirect('main');
                } else if ($user->hasRole('registrador')) {
                    return redirect('select_lang');
                } else {
                    return back()->withErrors(['email' => 'Incorrect user permissions'])
                        ->withInput(request(['email']));
                }
              }
               else
              {
                  Auth::logout(); //to logout user
            return back()->withErrors(['email' => 'Account Deactivated please contact admin'])->withInput(request(['email'])); // error message if user deactivated.
                  }
                //Terminan cambios RDAN
    
            } else {
                return back()->withErrors(['email' => 'Incorrect user permissions'])
                    ->withInput(request(['email']));
            }
        }

  • Index view:索引视图:
 

  @foreach($users ?? '' as $user)   
      <tr>
         <td class="small  text-center">{{$user->name}}</td>
         <td class="small  text-center">{{$user->email}}</td>
         <td class="small  text-center">
         @foreach($user->roles as $role)
            {{$role->nombre_rol. '-'. ' '}}
         @endforeach
         </td>

         <td class="small  text-center">
             <input {{$user->active=='1'?'checked':''}} type="checkbox" name="active" value="1">
          </td>

          <td class="small  text-center">
              <div class="row">
                   <div >
                      <!--VIEW-->
                      <button onclick="verUsuario({{$user->id}})" class="btn btn-sm btn-outline-success m-1">
                       <i class="bi bi-eye align-content-center"></i>
                      </button>
                  </div>    
                   <div >
                       <!--EDIT-->
                       <button onclick="editarUsuario({{$user->id}})" class="btn btn-sm btn-outline-warning m-1">
                         <i class="bi bi-pencil-fill align-content-center"></i>
                        </button>
                        </div>
                           <!--Delete-->
                           <form id="delete"  action="{{url('Usuario/'.$user->id)}}" method="post">
                            {{csrf_field()}}
                            {{method_field('DELETE')}}
                             <button type="submit" onclick="return confirm('Va a borrar la usuario {{$user->name}} ¿está seguro?')" class="btn btn-sm btn-outline-danger m-1"><i class="bi bi-trash-fill align-content-center"></i></button>
                         </form>
                      </div>
                  </td>
              </tr>
      @endforeach

In MySQL database I added an 'active' column of type 'tinyint(1)' in the table 'users'.在 MySQL 数据库中,我在“用户”表中添加了“tinyint(1)”类型的“活动”列。 I added it in the view where the user 'admin' can enable or disable a user with a checkbox.我将它添加到用户“管理员”可以使用复选框启用或禁用用户的视图中。 I don't understand how to make sure that if it is not checked the value change for a 0. The checkbox is not in a form, it's in my index as a column.我不明白如何确保如果未检查 0 的值更改。复选框不在表单中,它在我的索引中作为一列。 Can someone help me?有人能帮我吗?

I think you forgot the round brackets in the condition我想你忘记了条件中的圆括号

<input {{((int)$user->active==1)?'checked':''}} type="checkbox" name="active" value="1">

If not that is what you looking for and the active attribute is always false maybe you forgot to add the active attribute in the model User如果不是这就是您要查找的内容并且 active 属性始终为 false 可能您忘记在 model 用户中添加 active 属性

<element id="blabla" onclick="uncheck()" />

JS: JS:

getElementById(blabla)
blabla.checked = true/false

PHP: PHP:

DB::table('tablename')->update(['checkbox' => 0/1]);

Also it would be preferable if you use a boolean instead of an integer because it's the real type you need and it's more simple, logic and maybe, secure此外,如果您使用 boolean 而不是 integer 会更可取,因为它是您需要的真正类型,而且它更简单、更符合逻辑并且可能更安全

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM