如何处理 Laravel 中未选中的复选框? [英] How to handle unchecked checkboxes in Laravel?

查看:16
本文介绍了如何处理 Laravel 中未选中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Laravel 中有一个带有多个复选框的编辑表单.当我选中复选框并更新记录时,它可以正常工作,但如果我取消选中该复选框,它不会返回任何值,因此记录不会更新并且该值在数据库中保持为真.

I have an edit form with several checkboxes in Laravel. When I check the boxes and update the record, it works correctly, but if I uncheck the box it doesn't return any value so the record doesn't update and the value remains true in the database.

如何处理 Laravel 中未选中的复选框?

How can I handle unchecked checkboxes in Laravel?

推荐答案

来自 关于复选框的 mozilla 文档:

如果一个复选框在提交表单时未选中,则没有提交给服务器的值来表示其未选中状态(例如 value=unchecked);该值根本不提交给服务器.

If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state (e.g. value=unchecked); the value is not submitted to the server at all.

因此,以下内容可以解决问题:

So, the following will do the trick:

$isChecked = $request->has('checkbox-name');

这篇关于如何处理 Laravel 中未选中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆