未选中的复选框返回空值 [英] Unchecked checkbox returning null value

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

问题描述

我有一组复选框,当检查它们时,它们将值传递为 1,否则它们将值传递为 0.但是,不是将未选中的复选框的值发送为0",而是发送的值为NULL"'.

I have a set of checkboxes which when checked they pass the value as 1, otherwise they pass the value as 0. However, instead of sending the value of the unchecked checkboxes as '0', the value being sent is 'NULL'.

我有以下 JS 代码,应该相应地将值设置为 0/1,但是该值仍然作为 NULL 发送.有什么办法可以确保在未选中复选框的情况下传递的值为 0?

I have the following JS code in place that should set the value to 0/1 accordingly, however still the value is sent as NULL. Is there anything that can be done to make sure that the value passed in case of an unchecked checkbox is 0?

$('#cb1, #cb2, #cb3, #cb4').on('click', function ()
    $(this).val(this.checked ? 1 : 0);
});

更新这是我的 html:

<input type="checkbox" name="cb1" id="cb1" value="1" checked />
<input type="checkbox" name="cb2" id="cb2" value="1" checked />
<input type="checkbox" name="cb3" id="cb3" value="1" checked />
<input type="checkbox" name="cb4" id="cb4" value="1" checked />

它们都被默认选中.

如果其中之一未选中,MySQL 将报告以下错误:0SQLSTATE[23000]:违反完整性约束:1048 列ColumnName"不能为空.

If one of them is unchecked, MySQL is reporting the following error: 0SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ColumnName' cannot be null.

这是因为列被设置为不为空.即使我已将默认值设置为 0,一旦我单击提交按钮,我仍然会收到错误消息.我试图删除 Not NULL 属性,但不是将值预填充为 0,而是数据库中的输入为 NULL.

This is because the columns are set to set not to be Null. And even though I have set the default value to 0, once I click on the submit button I still get the error. I tried to remove the Not NULL property however rather than prefilling the value as 0, the input was NULL in the database.

推荐答案

如果复选框未选中,则不会发送,因此如果未选中,则将其值设置为 0 无济于事 - 它将始终返回 NULL.

If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL.

有两种方法可以轻松解决此问题:

There are two ways to fix this easily:

1) 假设 PHP 参数中的 NULL 表示复选框未选中.如果页面上并不总是存在该复选框,则这可能是有问题的.听起来,复选框的数量是可变的,所以这可能行不通.

1) Assume a NULL in the PHP params means the checkbox is unchecked. If the checkbox doesn't always exist on the page, this could be problematic. By the sounds of it, there is a variable number of checkboxes, so this probably won't work.

2) 添加与复选框同名且值为 0 BEFORE 复选框的隐藏输入.如果未选中复选框,将使用隐藏字段值,如果选中,将使用复选框值.

2) Add a hidden input that has the same name as the checkbox with the value of 0 BEFORE the checkbox. If the checkbox is unchecked, the hidden field value will be used, if it is checked the checkbox value will be used.

<input type="hidden" name="checkbox_1" value="0">
<input type="checkbox" name="checkbox_1" value="1">

注意:如果您的名字是数组形式(即它们中有方括号),这将不起作用,因为隐藏字段也会增加数组计数.

Note: If your names are in an array form (ie they have square brackets in them), this won't work, as the hidden fields will increment the array count as well.

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

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