【问题标题】:Laravel 7 - Uncheck the checkbox in AJAXLaravel 7 - 取消选中 AJAX 中的复选框
【发布时间】:2022-01-10 15:25:28
【问题描述】:

我不太擅长 ajax。我想取消选中“活跃”用户的复选框。

  • 我的 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']));
            }
        }

  • 索引视图:
 

  @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

在 MySQL 数据库中,我在“users”表中添加了“tinyint(1)”类型的“活动”列。我将它添加到用户“管理员”可以使用复选框启用或禁用用户的视图中。我不明白如何确保如果未检查 0 的值更改。复选框不在表单中,它在我的索引中作为一列。有人可以帮我吗?

【问题讨论】:

    标签: php ajax laravel checkbox unchecked


    【解决方案1】:

    我想你忘记了条件中的圆括号

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

    如果不是这就是您要寻找的,并且 active 属性始终为 false 也许你忘记在模型 User 中添加 active 属性

    【讨论】:

      【解决方案2】:
      <element id="blabla" onclick="uncheck()" />
      

      JS:

      getElementById(blabla)
      blabla.checked = true/false
      

      PHP:

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

      此外,如果您使用布尔值而不是整数会更可取,因为它是您需要的真实类型,而且它更简单、更符合逻辑并且可能更安全

      【讨论】:

      • 它抛出了这个错误 array_merge (): Expected parameter 1 to be an array, int given
      • 所以给一个数组...
      • 我已经这样做了,但它不能正常工作,它只是禁用了所有框,我无法再次激活它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 2013-11-04
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多