首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何选中复选框和取消选中这两个值?

如何选中复选框和取消选中这两个值?
EN

Stack Overflow用户
提问于 2018-06-06 04:54:34
回答 2查看 479关注 0票数 0

我正在研究前十字韧带。在每个角色中都有一组权限。我代表所有使用复选框的权限。当我提交表单时,它只检查权限的值。但是我想要得到所有选中和未选中的值。

当我点击更新按钮时,它只显示这些值

代码语言:javascript
复制
[
    {
        "read": "true",
        "create": "true"
    }
]

我想要得到这样的价值。

代码语言:javascript
复制
[
    {
        "read": "true",
        "create": "true",
        "delete": "false",
        "update": "false"
    }
]

我正在尝试使用JQuery,但没有得到预期的结果。如果有任何其他方法,请提出建议。

我的视图部分。

代码语言:javascript
复制
@foreach($role->permissions as $key=>$value)
    <td><input type="checkbox" name="permisssions[{{$key}}]" class="role" value="true"  {{ $value==1 ? 'checked' : '' }}></td>
@endforeach

我的控制器部分。

代码语言:javascript
复制
public function role_permissions_update(Request $request, $id)
{
    return array($request->permisssions);

}
EN

回答 2

Stack Overflow用户

发布于 2018-06-06 05:12:01

而不是在UI中做这项工作-你可以考虑在控制器之前在请求消毒器中做。就像这样..。

MyController.php

代码语言:javascript
复制
<?php
namespace App\Http\Controllers;
use App\Http\Requests\MyRequest;

class MyController{
    public function myMethod(MyRequest $request){
        //write code only for valid, sanitized requests.
    }
}

MyRequest.php

代码语言:javascript
复制
namespace App\Http\Requests;

class MyRequest extends SanitizedRequest{

protected static $bools = ['check_box_a','check_box_b'];

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules(){
    return [
        //
    ];
   }
}

SanitizedRequest.php (我猜可能是抽象的)

代码语言:javascript
复制
<?php
namespace App\Http\Requests;


use Illuminate\Foundation\Http\FormRequest;

class SanitizedRequest extends FormRequest{

/**
 * @var bool
 */
private $clean = false;

/**
 * @var array
 */
protected static $arrays = [];

/**
 * @var array
 */
protected static $bools = [];

/**
 * @var array
 */
protected static $nullables = [];

/**
 * @var array
 */
protected static $removeables = [];



/**
 * We will do ALL of our security from middleware.
 *
 * @return bool
 */
public function authorize(){
    return true;
}

/**
 * @return array
 */
public function all($keys = null){
    return $this->sanitize(parent::all($keys));
}

/**
 * @param array $inputs
 *
 * @return array
 */
protected function sanitize(Array $inputs){

    if($this->clean){
        return $inputs;
    }

    // Get the request after middlewares (ie: the way it should be).

    $inputs = $this->request->all();

    $inputs = self::fillStaticDefaults($inputs);
    $inputs = self::numbers($inputs);
    $inputs = self::arrays($inputs);
    $inputs = self::bools($inputs);
    $inputs = self::removeables($inputs);

    if(method_exists($this, 'dynamicDefaults')){
        $inputs = $this->dynamicDefaults($inputs);
    }

    if(method_exists($this, 'preProcessInputs')){
        $inputs = $this->preProcessInputs($inputs);
    }

    $inputs = self::nullables($inputs);

    $this->replace($inputs);
    $this->clean = true;

    return $inputs;

}

/**
 * @param $inputs
 *
 * @return mixed
 */
private static function fillStaticDefaults($inputs){
    if(empty(static::$defaults)){
        return $inputs;
    }

    foreach(static::$defaults as $key => $val){
        if(empty($inputs[$key])){
            $inputs[$key] = $val;
        }
    }

    return $inputs;
}


private static function numbers($inputs){
    foreach($inputs as $k => $input){
        if(is_numeric($input) and !is_infinite($inputs[$k]*1)){
            $inputs[$k] *=1;
        }
    }

    return $inputs;
}

/**
 * @param array $inputs
 *
 * @return array
 */
private static function arrays(Array $inputs){
    foreach(static::$arrays as $array){
        if(empty($inputs[$array])
           or !is_array($inputs[$array])
        ){
            $inputs[$array] = [];
        }
    }

    return $inputs;
}

/**
 * @param array $inputs
 *
 * @return array
 */
private static function bools(Array $inputs){
    foreach(static::$bools as $bool){
        $inputs[$bool] = (!empty($inputs[$bool]) and $inputs[$bool] != '0' and strtolower($inputs[$bool]) != 'false' and $inputs[$bool] != 0) ? 1:0;
    }

    return $inputs;
}

/**
 * @param array $inputs
 *
 * @return array
 */
private static function nullables(Array $inputs){

    foreach($inputs as $k => $input){
        if(is_array($input)){
            $input = self::nullables($input);
            $inputs[$k] = $input;
        }
        if(empty($input) and $input !== 0 and !(in_array($k, static::$bools) or in_array($k, static::$nullables))){
            unset($inputs[$k]);
        }
    }

    foreach(static::$nullables as $null){
        $inputs[$null] = (!empty($inputs[$null])) ? $inputs[$null]:null;
    }

    return $inputs;
}

/**
 * @param array $inputs
 *
 * @return array
 */
private function removeables(Array $inputs){
    if(pos(static::$removeables) == '*'){
        foreach($inputs as $k => $v){
            if(!is_array($v)){
                if(empty($v)){
                    unset($inputs[$k]);
                }
            }else{
                $inputs[$k] = self::removeables($v);
            }
        }

        return $inputs;
    }

    foreach(static::$removeables as $removeable){
        if(empty($inputs[$removeable])){
            unset($inputs[$removeable]);
        }
    }

    return $inputs;
}

}
票数 0
EN

Stack Overflow用户

发布于 2018-06-06 06:08:46

如果唯一的问题是获取未选中复选框的值,则可以像这样使用隐藏输入

代码语言:javascript
复制
<input type="hidden" name="permisssions[{{$key}}]" class="role" value="true"  {{ $value==1 ? 'checked' : '' }}>
        <input type="checkbox" name="permisssions[{{$key}}]" class="role" value="true"  {{ $value==1 ? 'checked' : '' }}>

如果他们选中了复选框,你会得到它的值,否则你会得到隐藏输入的值

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50708778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档