繁体   English   中英

如何自动选中和取消选中复选框

[英]How to check and uncheck checkbox automatically

我正在做一个页面,我可以在其中显示带有复选框的数据列表。 然后我将自动检查数据库,如果有数据,则将选中该复选框。 我现在想做的是,假设该复选框已自动选中,当我自己取消选中并保存时,它将更新数据库中的数据。

我的编码示例:

<form action="{{URL::to('/granted/'.$d->id)}}" method="get">
    <input name="_token" type="hidden" value="{{ csrf_token() }}"/>
    @foreach($var as $v)
    <table style="margin-left:20px;">
         <caption>{{$v->module_groupname}}</caption>
         @foreach($name as $n)
         <?php if($n->module_groupname == $v->module_groupname){?>
         <tr><td width="80"><input type="checkbox" name="module_code[]" value="{{ $n->module_code }}"
         <?php foreach($priv as $p){
         if($p->user_id == $d->id){
             if($p->module_code == $n->module_code){
                 echo 'checked="checked"';
             }
         }
     }?>></td>
     <td width="150">{{$n->module_groupname}}</td>
     <td width="200">{{ $n->module_desc}}</td>
    </tr> <?php } ?>
 @endforeach
</table>
<hr>
@endforeach
<input type="submit" value="select" class="btn btn-s btn-success">
</form>

建议我查找PLUS语句oracle,但我找不到如何

对于自动检查,您需要设置选中该复选框的属性

https://www.w3schools.com/tags/att_input_checked.asp

为了通过js做到这一点,请遵循以下网址! 说setAttribute属性

https://www.w3schools.com/jsref/met_element_setattribute.asp

希望这个能对您有所帮助

<input type="checkbox" @if(compare) checked @endif />

我建议您看一下这段代码,然后尝试重新格式化自己的代码。 您正在使用刀片,因此请随处使用。

<form action="{{URL::to('/granted/'.$d->id)}}" method="get">
{{ csrf_field() }}
@foreach($var as $v)
    <table style="margin-left:20px;">
        <caption>{{$v->module_groupname}}</caption>
        @foreach($name as $n)
            @if($n->module_groupname == $v->module_groupname)
                <tr>
                    <td width="80"><input type="checkbox" name="module_code[]" value="{{ $n->module_code }}" {{ $priv->contains('user_id', $d->id) && $priv->contains('module_code', $n->module_code) ? 'checked' : '' }}></td>
                    <td width="150">{{$n->module_groupname}}</td>
                    <td width="200">{{ $n->module_desc}}</td>
                </tr>
            @endif
        @endforeach
    </table>
    <hr>
@endforeach
<input type="submit" value="select" class="btn btn-s btn-success">

我认为如果$ prev变量是模型,则可以使用contains函数。

我没有测试此代码,因此请尝试是否有效

暂无
暂无

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

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