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

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

问题描述

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



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

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



UPDATED
这是我的html:

 < input type =checkboxname =cb1id =cb1value =1checked / 
< input type =checkboxname =cb2id =cb2value =1checked />
< input type =checkboxname =cb3id =cb3value =1checked />
< input type =checkboxname =cb4id =cb4value =1checked />

默认情况下都会检查。



如果其中一个未选中,MySQL会报告以下错误:
0SQLSTATE [23000]:完整性约束违例:1048列'ColumnName'不能为空。



这是因为列被设置为不设置为Null。即使我已经将默认值设置为0,一旦我点击提交按钮,我仍然得到错误。我尝试删除非NULL属性,而不是预填充值为0,输入在数据库中为NULL。

解决方案

如果一个复选框被取消选中,它不会被发送,所以如果没有选中则将值设置为0不会帮助 - 它总是返回NULL。



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



1)假设PHP params中的NULL表示复选框未被选中。如果该复选框并不总是存在于页面上,这可能是有问题的。根据它的声音,有一个可变数量的复选框,所以这可能不工作。



2)添加一个隐藏的输入,选中值为0 BEFORE 的复选框。如果未选中该复选框,则将使用隐藏的字段值,如果选中了复选框值,则会使用。

 < ; input type =hiddenname =checkbox_1value =0> 
< input type =checkboxname =checkbox_1value =1>

注意:如果你的名字是一个数组形式将不工作,因为隐藏的字段也将增加数组计数。


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'.

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);
});

UPDATED Here is my 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 />

They are all checked by default.

If one of them is unchecked, MySQL is reporting the following error: 0SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ColumnName' cannot be 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.

解决方案

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) 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) 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.

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

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