Javascript 如何在laravel中显示选中的复选框
您好,我正在处理多个复选框,复选框的值以这种格式存储在我的数据库2,3,4中Javascript 如何在laravel中显示选中的复选框,javascript,php,mysql,ajax,laravel,Javascript,Php,Mysql,Ajax,Laravel,您好,我正在处理多个复选框,复选框的值以这种格式存储在我的数据库2,3,4中 我的刀片文件的代码:- <table class="table table-striped"> @foreach($var as $vars) <tbody> <tr> <td>
我的刀片文件的代码:-
<table class="table table-striped">
@foreach($var as $vars)
<tbody>
<tr>
<td>
<input type="checkbox" class="larger" name="process[]" value="" {{ ($vars->process == 1 ? 'checked' : '')}}>Cold/Hot Forging
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="larger" name="process[]" value="" {{ ($vars->process == 2 ? 'checked' : '')}}>Injection Molding
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="larger" name="process[]" value="" {{ ($vars->process == 3 ? 'checked' : '')}}>Precision Machining
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="larger" name="process[]" value="" {{ ($vars->process == 4 ? 'checked' : '')}}>Sheet Metal Pressing
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="larger" name="process[]" value="" {{ ($vars->process == 5 ? 'checked' : '')}}>Sub-assembly
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="larger" name="process[]" value="" {{ ($vars->process == 6 ? 'checked' : '')}}>Surface tretment
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="larger" name="process[]" value="" {{ ($vars->process == 7 ? 'checked' : '')}}> Other
</td>
</tr>
</tbody>
@endforeach
</table>
首先,使用所有数据创建一个数组
<?php
$all_processvalue = array();
foreach($processvalue as $pvalue){
$all_data[] = $pvalue->id;
}
?>
现在创建复选框
@foreach ($vars as $var)
{{ Form::checkbox('var[]', $var->id, in_array($var->id, $all_processvalue )) }}
{{ Form::label('var', $var->name) }}<br>
@endforeach
@foreach($var作为$var)
{{Form::checkbox('var[]',$var->id,在_数组中($var->id,$all_processvalue))}
{{Form::label('var',$var->name)}
@endforeach
您的解决方案不适用于我等等,让我检查一下您存储的是逗号分隔的值字符串(2,3,4
)?如果$vars->process==1
,这永远不会是真的$vars->process='1,2,3'
。
@foreach ($vars as $var)
{{ Form::checkbox('var[]', $var->id, in_array($var->id, $all_processvalue )) }}
{{ Form::label('var', $var->name) }}<br>
@endforeach