[英]How can I post a checkbox value to a controller that saves value to session?
[英]In Laravel 5.1 Can't post value of checkbox
我在表單中有2個復選框,我無法發布它們的值。 我已經嘗試過了:
<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。
嘗試這個
$project = $request->input('project');
$tobeinvoiced = $project['tobeinvoiced'];
基本上,您使用的命名約定使它成為數組,並且您不能在laravel的請求輸入中直接調用數組
希望這可以幫助。
我已經找到解決問題的方法,這是鏈接: 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');
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.