duanchao1002 2015-11-01 19:42
浏览 36
已采纳

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

I have 2 checkboxs inside a form, I can't post value of them. 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:

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

  • 写回答

2条回答 默认 最新

  • douya7309 2015-11-04 18:54
    关注

    I've found the solution to my problem, this is the link : 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');
                    });
                });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大