如何从复选框元素中获取默认值?

时间:2022-11-27 17:33:16

Below is my code:

以下是我的代码:

<INPUT TYPE="checkbox" NAME="cb" value="1">

After form is submitted, I get cb==1, if cb element is checked,

提交表单后,如果选中cb元素,我会得到cb == 1,

I want to get cb==0, if cb element is not checked. How to implement it?

如果未检查cb元素,我想得到cb == 0。怎么实现呢?

BTW: can't change receive page, like:

顺便说一句:无法更改接收页面,如:

if(cb==null)cb=0;// no no no..

I want implement it at web front client.

我想在Web前端客户端实现它。

I tried like this:

我试过这样的:

<INPUT TYPE="hidden" NAME="cb" value="0"> <!--add this line-->
<INPUT TYPE="checkbox" NAME="cb" value="1">

If cb is not checked, it can override <INPUT TYPE="hidden" NAME="cb" value="0"> else I can get cb==0.

如果没有选中cb,它可以覆盖否则我可以得到cb == 0。

but I feel this solution is not good.

但我觉得这个解决方案并不好。

any idea? thx~ :)

任何想法? thx~ :)


Thanks wowo_999 for comment:

感谢wowo_999评论:

"I'd just ignore the value of the checkbox period and use it to set the value of your hidden field based on if its checked or not. The way you suggest will send the value for cb 2x in your query string if the checkbox is checked." – wowo_999

“我只是忽略复选框周期的值,并根据是否检查隐藏字段的值来设置隐藏字段的值。如果复选框是复选框,您建议的方式是在查询字符串中发送cb 2x的值检查。” - wowo_999


In database,I set cb field default value as"0",but when I insert or update,if it is null,show: "java.sql.SQLException: Column 'cb' cannot be null",so,I need cb==0,not null.(I can't ignore cb field in sql query string,because I maybe need update cb to "0".)

在数据库中,我将cb字段默认值设置为“0”,但是当我插入或更新时,如果它为null,则显示:“java.sql.SQLException:列'cb'不能为空”,所以,我需要cb = = 0,而不是null。(我不能忽略sql查询字符串中的cb字段,因为我可能需要将cb更新为“0”。)

3 个解决方案

#1


1  

If you're going to use the checkbox's value to set the value of the hidden input, you might as well use javascript to set the value of the checkbox. For example using jQuery:

如果您要使用复选框的值来设置隐藏输入的值,您也可以使用javascript来设置复选框的值。例如使用jQuery:

$("input[type=checkbox]").change(function() {
    $(this).attr("value", $(this).attr("checked") ? 1 : 0);
});

Or, let the script set the value on page load as well:

或者,让脚本也设置页面加载的值:

$(function() {
    function updateCheckbox() {
        $(this).attr("value", $(this).attr("checked") ? 1 : 0);
    }
    // Run whenever checkbox is ticked or unticked
    $("input[type=checkbox]").change(updateCheckbox);
    // Run on page load
    updateCheckbox();
});

And now you can use regular HTML markup too, without any hidden inputs and without value attributes:

现在你也可以使用常规的HTML标记,没有任何隐藏的输入和没有值属性:

<input type="checkbox" name="cb" />

Or have it checked by default:

或者默认选中它:

<input type="checkbox" name="cb" checked />

#2


1  

you can use javascript to check whether its unchecked and change the value to 0 and set it checked, on submit.

您可以使用javascript检查是否取消选中,并在提交时将值更改为0并将其设置为已选中。

#3


0  

You can't default in the DB as "0"?

您不能在数据库中默认为“0”?

#1


1  

If you're going to use the checkbox's value to set the value of the hidden input, you might as well use javascript to set the value of the checkbox. For example using jQuery:

如果您要使用复选框的值来设置隐藏输入的值,您也可以使用javascript来设置复选框的值。例如使用jQuery:

$("input[type=checkbox]").change(function() {
    $(this).attr("value", $(this).attr("checked") ? 1 : 0);
});

Or, let the script set the value on page load as well:

或者,让脚本也设置页面加载的值:

$(function() {
    function updateCheckbox() {
        $(this).attr("value", $(this).attr("checked") ? 1 : 0);
    }
    // Run whenever checkbox is ticked or unticked
    $("input[type=checkbox]").change(updateCheckbox);
    // Run on page load
    updateCheckbox();
});

And now you can use regular HTML markup too, without any hidden inputs and without value attributes:

现在你也可以使用常规的HTML标记,没有任何隐藏的输入和没有值属性:

<input type="checkbox" name="cb" />

Or have it checked by default:

或者默认选中它:

<input type="checkbox" name="cb" checked />

#2


1  

you can use javascript to check whether its unchecked and change the value to 0 and set it checked, on submit.

您可以使用javascript检查是否取消选中,并在提交时将值更改为0并将其设置为已选中。

#3


0  

You can't default in the DB as "0"?

您不能在数据库中默认为“0”?