wood burning stoves*
The moose likes JSP and the fly likes getting checkbox value even when unchecked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "getting checkbox value even when unchecked" Watch "getting checkbox value even when unchecked" New topic
Author

getting checkbox value even when unchecked

changhyun moon
Greenhorn

Joined: Oct 19, 2008
Posts: 7
Dear peeps,
I have a problem with getting checkbox value.
I have a list of checkbox
and I need to get the value of checkbox
'1' when checked, and '2' when unchecked.

But as I know, checkbox deliver their value only when it is checked.
How can I approach this problem?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

"ch moon", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

As you seem to already know, checkboxes will only submit a value when they are checked. There is no way to change this behavior.

You'll need to handle this on the server, perhaps by providing a default value when no value is submitted.

Or, perhaps you can consider using a set of radio buttons in place of the checkbox.
[ October 20, 2008: Message edited by: Bear Bibeault ]
changhyun moon
Greenhorn

Joined: Oct 19, 2008
Posts: 7
Thanks for your guide and right reply,
I thought about it.
However, I need to set the values with proper list already existed in the server.
I believed that checking checked or unchecked is waste of process while I set the list.
Is there anyway in the javascript way to control unchecked value to be delivered as '2'(as I described on the question)?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

I repeat:
There is no way to change this behavior.

You can consider switching to radio buttons, as I suggested, or you could create a hidden element to submit an appropriate value. But there is no way to force an unchecked checkbox to return a value when unchecked.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by changhyun moon:
I believed that checking checked or unchecked is waste of process while I set the list.
The amount of time spent based on unsupported beliefs often amazes me. Just write the if-statement already. There's no reason to look for workarounds for an if-statement.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Originally posted by Paul Clapham:
The amount of time spent based on unsupported beliefs often amazes me. Just write the if-statement already. There's no reason to look for workarounds for an if-statement.

Quoted for truth.
in search for java
Greenhorn

Joined: Sep 30, 2008
Posts: 1
You can use Java script to get the values and put the values in session. when next time page loads or do anything its still in session and then you can put logic to again change/retrieve the value
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

"in search for java", please check your private messages for an important administrative matter.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Originally posted by in search for java:
You can use Java script to get the values and put the values in session.
No, you can't. JavaScript has no access to the session whatsoever.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting checkbox value even when unchecked