dongqie2010 2017-03-27 05:21
浏览 159
已采纳

laravel中的复选框问题

I have check box inside a foreach loop so that i want to checkbox checked.

<?php
    $heads = TCG\Voyager\Models\Head::all();
    $i=0;                                 
    foreach($heads as $heads){
        $headvalues=explode(',',$test->reporting_head);
        echo $headvalues[$i]; 
        echo '<br>';
        echo $heads->id;
?>
<input type="checkbox"  name="check_list[]" value="{{$heads->id}}_{{$heads->email}}" <?php echo (($headvalues[$i]==$heads->id)? 'checked' : ''); ?>><label>{{$heads->headname}}</label>
<?php 
        $i++;
    }
?>

Here echo $headvalues[$i] are 1, 2 ,3 and echo $heads->id are 1,3,2.so i want to checked all this check boxes but now only one check box is checked.i think my logic has issue.I tried the following

<?php echo (($headvalues[$i]==$heads->id)? 'checked' : ''); ?>

Please help me

  • 写回答

1条回答 默认 最新

  • dongzhidian3538 2017-03-27 12:12
    关注

    Instead of

    <?php echo (($headvalues[$i]==$heads->id)? 'checked' : ''); ?>
    

    use

    <?php echo (in_array($heads->id, $headvalues)? 'checked' : ''); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
  • ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
  • ¥15 io.jsonwebtoken.security.Keys
  • ¥15 急,ubuntu安装后no caching mode page found等