简体   繁体   English

在Laravel 5.1中无法发布复选框的值

[英]In Laravel 5.1 Can't post value of checkbox

I have 2 checkboxs inside a form, I can't post value of them. 我在表单中有2个复选框,我无法发布它们的值。 I have already tried this: 我已经尝试过了:

<div class="form-group col-md-4">
                <label class="col-md-6" for="invoiced">
                    <input type="checkbox" id="invoiced" value="false" name="project[invoiced]"> 
                     Invoiced </label>
                <label class="col-md-6" for="tobeinvoiced">
                    <input type="checkbox" id="tobeinvoiced" value="true" name="project[tobeinvoiced]" checked> 
                    To be Invoiced </label>
            </div>

with this script that changes the value of 2 checkboxes in true or false: 使用此脚本可以更改2个复选框的值为true或false:

<script type="text/javascript">
            $('#tobeinvoiced').change(function(){
                cb = $(this);
                cb.val(cb.prop('checked'));
            });
            $('#invoiced').change(function(){
                cb = $(this);
                cb.val(cb.prop('checked'));
            });
            </script>

but when I submit, the values passed are set to null. 但是当我提交时,传递的值设置为null。

Try this 尝试这个

$project = $request->input('project');
$tobeinvoiced = $project['tobeinvoiced'];

Basically the naming convention you are using makes it an array and you cant call an array directly in request input in laravel 基本上,您使用的命名约定使它成为数组,并且您不能在laravel的请求输入中直接调用数组

Hope this helps. 希望这可以帮助。

I've found the solution to my problem, this is the link : Solution . 我已经找到解决问题的方法,这是链接: Solution

My code became: 我的代码变成:

<label class="col-md-6" for="invoiced">
   <input type="checkbox" key="invoiced"/>
   <input type="hidden" id="invoiced" value="0" name="project[invoiced]"> 
   Invoiced </label>
<label class="col-md-6" for="tobeinvoiced">
   <input type="checkbox" key="tobeinvoiced" checked/>
   <input type="hidden" id="tobeinvoiced" value="1" name="project[tobeinvoiced]"> 
   To be Invoiced </label>

with this script: 使用此脚本:

 $(document).ready(function () {
                $('[key]').change(function () {
                    var key = $(this).attr('key');
                    $($('[name="project[' + key + ']"]')).val($(this).is(':checked') ? 'true' : 'false');
                });
            });

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

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