Why can't radio buttons be “readonly”?

前端 未结 13 929
误落风尘
误落风尘 2020-11-27 10:45

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 t

相关标签:
13条回答
  • 2020-11-27 11:00

    For the non-selected radio buttons, flag them as disabled. This prevents them from responding to user input and clearing out the checked radio button. For example:

    <input type="radio" name="var" checked="yes" value="Yes"></input>
    <input type="radio" name="var" disabled="yes" value="No"></input>
    
    0 讨论(0)
  • 2020-11-27 11:00

    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.

    0 讨论(0)
  • 2020-11-27 11:03

    Radio buttons would only need to be read-only if there are other options. If you don't have any other options, a checked radio button cannot be unchecked. If you have other options, you can prevent the user from changing the value merely by disabling the other options:

    <input type="radio" name="foo" value="Y" checked>
    <input type="radio" name="foo" value="N" disabled>
    

    Fiddle: http://jsfiddle.net/qqVGu/

    0 讨论(0)
  • 2020-11-27 11:04

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

    <img src="itischecked.gif" alt="[x]" />radio button option
    

    Best Regards.

    0 讨论(0)
  • 2020-11-27 11:06

    I've faked readonly on a radio button by disabling only the un-checked radio buttons. It keeps the user from selecting a different value, and the checked value will always post on submit.

    Using jQuery to make readonly:

    $(':radio:not(:checked)').attr('disabled', true);
    

    This approach also worked for making a select list readonly, except that you'll need to disable each un-selected option.

    0 讨论(0)
  • 2020-11-27 11:07

    This is the trick you can go with.

    <input type="radio" name="name" onclick="this.checked = false;" />
    
    0 讨论(0)
提交回复
热议问题