【问题标题】:Find CheckBox state - laravel查找复选框状态 - laravel
【发布时间】:2017-11-18 13:04:34
【问题描述】:

如何在 laravel 中找到复选框的状态?我想根据复选框的状态执行请求。这就是我目前正在做的事情。

但只有代码的 else 部分继续运行。我做错了什么?

if(Input::get('check', false))                  
{                
    //do this
}
else 
{
    //do that             
}

更新

$test = new Test(array(            

             'id' => $no,
             $checkbox = $request['myCheck'] ? 1 : 0;

                 if ($checkbox == 1) 
                 {
                   $set_amt= $label- $figure;

                 } else {
                   $set_amt = $get_all;

                 }
             'amount' => $amount,

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您可以在控制器中执行此操作:

    public function store(Request $request)
    {
        $checkbox = $request['check'] ? 1 : 0;
    
        if ($checkbox == 1) 
        {
            //
        } else {
            //
        }
    }
    

    在你看来你是这样的:

    {!! Form::label('check', 'Checkbox Label: ')!!}
    {!! Form::check('check', null, isset($model_name) ? $model_name->check : 0, ['class' => 'form-control' ]) !!}
    

    如果你不使用 Form 外观,那么在视图中这样做:

    <label for="check">Checkbox Label: </label>
    <input class="form-control"
           value="1"
           name="check"
           type="checkbox"
           @if(isset($model) && $model->check == 1) checked @endif
    >
    

    更新

    我不知道你想达到什么目的,所以我现在只是猜测:

    public function store(Request $request)
    {
    
        $checkbox = $request['myCheck'] ? 1 : 0;
    
        // I can't see in your code what are these so I just 
        // guessed that they are maybe part of $request:
        $set_amt = null;
        $label = $request['label'];
        $figure = $request['figure'];
        $get_all = $request['get_all'];
    
        if ($checkbox == 1) 
        {
    
           $set_amt = $label - $figure;
    
        } else {
    
           $set_amt = $get_all;
    
        }
    
        $test = new Test([            
                 'id' => $no,
                 'set_amt' => $set_amt,
                 'amount' => $amount
             ]);
    
        $test->save();
    
        return redirect('/your_route_name');
    }
    

    【讨论】:

    • 如何在 if 语句中使用它?
    • 已更新答案...如果您需要更多帮助,请写在 cmets 中。
    • 请将您的视图更新为 html。我不使用 php 表单,而且我在这里遇到语法错误$checkbox = $request['check'] ? 1 : 0;
    • 请稍等。对于该错误,您必须在函数中包含 $request,然后尝试 dd($request->all()) 并查看复选框的名称并将该名称放入:$checkbox = $request['checkbox_name'] ? 1 : 0;
    • 这就是我所做的。我有 $request 那里。看看我的更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多