![](/img/trans.png)
[英]Update a JS array value with a global variable based on checkbox (true/false) form input
[英]Laravel Array to Input checkbox true or false value
嗨,我有一行数据,我希望允许用户单击打开/关闭数据的复选框以供使用。 我已经阅读了https://stackoverflow.com/a/43995087/14936674的答案,它工作正常,除了我的复选框值只有在选中后才能更新。 如果我想取消选中该框并对其进行更新,则不会对其进行更新。 目前我已经通过更新复选框进行了测试,但是它会更新并取消选中我的所有数据。
下面是我的刀片。php
<tbody>
@foreach($operatinghrs as $i => $operatinghr)
<tr id="{{ $operatinghr->{'opID'} }}">
<td>
<input type="hidden" name="operatinghrs[{{ $i }}][opID]" value="{{ $operatinghr->{'opID'} }}">{{ $operatinghr->{'day'} }}
</td>
<td>
<input type="time" id="start_time" name="operatinghrs[{{ $i }}][start_time]" min="00:00" max="23:59" value="{{ display24HTime(old('start_time', $operatinghr->start_time))}}">
</td>
<td>
<input type="time" id="end_time" name="operatinghrs[{{ $i }}][end_time]" min="00:00" max="23:59" value="{{ display24HTime(old('end_time', $operatinghr->end_time))}}">
</td>
<td>
<div class="switch">
<input type="checkbox" id="clinic_open" name="operatinghrs[{{ $i }}][clinic_open]" class="switch-input" value="1" {{ old('clinic_open', $operatinghr->clinic_open=="true") ? 'checked="checked"' : '' }}/>
<div class="circle"></div>
</div>
</td>
</tr>
@endforeach
</tbody>
以下是我的 controller class 中的代码:
public function update(Request $request)
{
foreach($request->get('operatinghrs', []) as $operatinghr) {
$db = new OperatingHour();
$db->where('opID', $operatinghr['opID'])->where('clinic_id', '=', $_SESSION['clinic_ID'])
->update(
[
'clinic_open' => $request->input('clinic_open') ? true : false,
]
);
}
return back()->with('success_message', 'Details updated successfully!');
}
如果您只想更新$operatinghr['clinic_open']
值为 true 的情况。
从 MDN 考虑这个:
如果一个复选框在提交表单时未选中,则没有任何值提交给服务器来表示其未选中 state(例如 value=unchecked); 该值根本不提交给服务器。 如果您想在未选中时提交复选框的默认值,您可以在表单中包含一个具有相同名称和值的表单,可能由 JavaScript 生成。
此外,对于特定条目,您必须访问$operatinghr
上的clinic_open
值作为$operatinghr['clinic_open']
。 不像$request->input('clinic_open')
所以解决方案必须是这样的:
// Get all Operating values.
$operatinghrs = $request->get('operatinghrs', []);
/*
* Filter values with clinic_open criteria.
* Checkbox values are only present when is checked.
* So isset control work for us.
*/
$operatingsForUpdate = array_filter($operatinghrs, function($op){
return isset($op['clinic_open']); // return only has clinic_open value.
});
// Loop for only for update operatings.
foreach($operatingsForUpdate as $operatinghr) {
// Update specific entry which contain clinic_open value is true.
}
使用复选框总是很棘手,复选框的默认值是on
和off
,但如果未选中该复选框,它将不会出现在请求数据中。
因此,您需要在尝试更新之前验证请求中是否存在复选框,如果不存在,则在数据库中保存字符串“off”或不保存任何内容并为该字段设置默认值(“off”),当您在编辑/更新表单中检索数据,所有带有“关闭”值的复选框都将被取消选中。
在$request
上使用has
方法,如下所示:
if($request->has('clinic_open')) {
foreach($request->get('operatinghrs', []) as $operatinghr) {
$db = new OperatingHour();
$db->where('opID', $operatinghr['opID'])->where('clinic_id', $_SESSION['clinic_ID'])
->update(['clinic_open' => $request->input('clinic_open')]);
}
}
一些建议:
避免在where
方法上使用 '=' 条件,laravel 总结它,节省时间和空间。
去掉复选框的value='1'
,把一些工作留给浏览器,少做点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.