2015-11-01 72 views
1

我有一个表单中的2个复选框,我无法发布它们的值。 我已经尝试过这样的:在Laravel 5.1不能发布复选框的值

<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> 

此脚本改变的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> 

但是当我提出,传递的值都设置为null。

+0

所以你要发送的复选框的价值?其他的表单元素还能正常工作吗? –

+0

是的,所有其他输入(文本,日期,数字)都可以正常工作,但复选框的值为空! –

+0

未选中复选框不作为参数发送请求。如果它存在于请求中,意味着复选框被标记,否则它没有标记。 – Svetoslav

回答

0

我找到了解决我的问题的方法,这是链接:Solution

我的代码变成了:

<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> 

这个脚本:

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

试试这个

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

基本上你正在使用的命名约定使其成为一个数组,你不能直接在请求输入调用数组中laravel

希望这有助于。

+0

我忘记提及我正在使用Json enctype,所以对于Input :: get('project'),我可以获取所有值输入,因此所有内容都适用于复选框值。 –