Below is my code:
<INPUT TYPE="checkbox" NAME="cb" value="1">
After form is submitted, I get cb==1
, if cb
element is checked,
I want to get cb==0
, if cb
element is not checked. How to implement it?
BTW: can't change receive page, like:
if(cb==null)cb=0;// no no no..
I want implement it at web front client.
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
.
but I feel this solution is not good.
any idea? thx~ :)
Thanks wowo_999 for ment:
"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
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".)
Below is my code:
<INPUT TYPE="checkbox" NAME="cb" value="1">
After form is submitted, I get cb==1
, if cb
element is checked,
I want to get cb==0
, if cb
element is not checked. How to implement it?
BTW: can't change receive page, like:
if(cb==null)cb=0;// no no no..
I want implement it at web front client.
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
.
but I feel this solution is not good.
any idea? thx~ :)
Thanks wowo_999 for ment:
"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
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".)
Share Improve this question edited Sep 22, 2014 at 9:53 farzad 8,8556 gold badges34 silver badges44 bronze badges asked Aug 2, 2010 at 18:38 KoerrKoerr 15.8k29 gold badges80 silver badges109 bronze badges 5- 2 The web browser will not send any value for a checkbox if it is not checked. ASP.NET internally uses the hidden element with the same name trick to send its default values for a checkbox. – Powerlord Commented Aug 2, 2010 at 18:41
- i see,any idea better than my solution? – Koerr Commented Aug 2, 2010 at 18:43
- 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 Commented Aug 2, 2010 at 19:19
- you use php, java or asp? – andres descalzo Commented Aug 2, 2010 at 19:27
- i use java. thank you for help :) – Koerr Commented Aug 2, 2010 at 19:43
3 Answers
Reset to default 1If 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:
$("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:
<input type="checkbox" name="cb" />
Or have it checked by default:
<input type="checkbox" name="cb" checked />
you can use javascript to check whether its unchecked and change the value to 0 and set it checked, on submit.
You can't default in the DB as "0"?