views:

1277

answers:

5

I would like to show a radio button, have its value submitted, but depending on the circumstances, have it not editable. Disabled doesn't work, because it doesn't submit th value (or does it?), and it grays out the radio button. Readonly is really what I'm looking for, but for some mysterious reason it doesn't work.

Is there some weird trick I need to pull to get readonly to work as expected? Should I just do it in javascript instead?

Incidentally, does anyone know why readonly doesn't work in radio buttons, while it does work in other input tags? Is this one of those incomprehensible omissions in the HTML specs?

+14  A: 

HTML Solution

You can disable it, and simply store its default value in a hidden field having the same name as the radio buttons (so your back-end logic need not change).
<input type='radio' name='val' value='oranges' disabled='disabled' />
<input type='hidden' name='val' value='oranges' />

Javascript Solution

You could simply respond to any click with an explicit request to uncheck the radio button itself.
<input type='radio' name='val' value='oranges' onclick='this.checked = false;' />

HTML/CSS Solution (Ugly Hack)

Using positioning in CSS, you could cover the buttons up with a transparent gif, making them unclickable.
<input type='radio' name='val' value='oranges' />
<img src='transparent.gif' class='positionOverRadio' />

Or you could simply go nuts and do all three ;) Your call.

Jonathan Sampson
The problem with your first solution is that if I want to enable the radiobutton through javascript, I suddenly have two fields with the same name, so I have to clean up one of them. Or use the hidden one for real and have the radio button just set the value of the hidden field. Ether way, it's just a bit more cumbersome than I'd like a radiobutton to be.Your second solution, although I don't doubt it works, sounds like a really ugly hack.So I guess the javascript solution is the only one that meets my criteria. I'll go with that.
mcv
Solution 2 is really a poor hack that probably won't work on less used browsers, such as "links". It also won't work on all browsers because you can TAB to it. It will just confuse the user.
Andreas Bonini
Solution 1 please! It's the only properly accessible one. And if you need to script it, it's only one more line to set the hidden field's enabledness to the opposite of the radio's enabledness.
bobince
That's a good point, bobince. Thanks.
mcv
I just noticed that solutions 2 and 3 have switched places in Jonathan's answer, so now the comments don't seem to make much sense.
mcv
+5  A: 

This is the trick you can go with.

<input type="radio" name="name" onclick="this.checked = false;" />
Sarfraz
Exactly what I was thinking, altho it would be best if you clarified that it should be `onClick="this.checked = <%=value%>"` of some variety :)
JustLoren
that is great to know. checked can have either of true or false values.
Sarfraz
Just a warning that this won't work for those of us who browse with javascript not turned on by default.
tloach
Not a problem for my case. We use tons of jQuery and Ajax already.
mcv
A: 

Try the attribute "diasble", but I think the you won't get the value of the radio buttons. Or set images instead like: radio button option

Best Regards.

Tim
This advice is good ..
infant programmer
oh, my html code was stripped:img src="itIsChecked.gif" alt="[x]" OptionChecked
Tim
A: 

What about capturing an "On_click()" event in JS and checking it like here?:

http://www.dynamicdrive.com/forums/showthread.php?t=33043

MInner
A: 

A fairly simple option would be to create a javascript function called on the form's "onsubmit" event to enable the radiobutton back so that it's value is posted with the rest of the form.
It does not seem to be an omission on HTML specs, but a design choice (a logical one, IMHO), a radiobutton can't be readonly as a button can't be, if you don't want to use it, then disable it.

wintermute