You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Ellingson, David" <Da...@NielsenMedia.com> on 2004/08/23 14:30:13 UTC

Default html:checkbox to true

It seems like this question would have been asked before, but I couldn't
find an answer in the archives.  I am able to successfully use the
html:checkbox tag.  However, this tag seems to be trickier to use when you
want to default the box to "checked".  Simply setting the form attribute to
true won't work, since an unchecked value will not be submitted and the form
attribute will remain true.  I've actually got this to work by what seems to
be a hack.  To force the browser to send a value when the box is unchecked,
I used the following Javascript:

if (document.getElementById("flag").checked == false) {
    // Force the browser to send a value
    document.getElementById("flag").checked = true;
    // Set the value to false
    document.getElementById("flag").value="false";
} 

This actually works fine, although the checkbox is toggled briefly to
"checked" before the form is submitted.  Is there a better to way to handle
checkboxes that need to default to "checked"?

Thanks,
Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Default html:checkbox to true

Posted by Lionel <co...@free.fr>.
Ellingson, David wrote:
> It seems like this question would have been asked before, but I
> couldn't find an answer in the archives.  I am able to successfully
> use the html:checkbox tag.  However, this tag seems to be trickier to
> use when you want to default the box to "checked".  Simply setting
> the form attribute to true won't work, since an unchecked value will
> not be submitted and the form attribute will remain true.

why don't you validate the value sent by the chekbox in the validate method
?
Set your form attribute to false if no parameter is sent by your form.




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Default html:checkbox to true

Posted by Rick Reumann <st...@reumann.net>.
Ellingson, David wrote:

> It seems like this question would have been asked before, but I couldn't
> find an answer in the archives.  I am able to successfully use the
> html:checkbox tag.  However, this tag seems to be trickier to use when you
> want to default the box to "checked".  Simply setting the form attribute to
> true won't work, since an unchecked value will not be submitted and the form
> attribute will remain true.  

You are having trouble because you need to use the reset() method to set 
all your checkboxes to false. Then, after your form submits only the 
ones checked will be set to true and everything will be fine (remember 
Struts always calls the reset method first then it populates your 
ActionForm with the submitted params). You do not need to do all those 
javascript hacks.

-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org