问题描述
我想显示一个单选按钮,提交它的值,但根据具体情况,它是否不可编辑.Disabled 不起作用,因为它不提交值(或者提交了?),并且它使单选按钮变灰.只读是我真正想要的,但由于某种神秘的原因它不起作用.
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 the value (or does it?), and it grays out the radio button. Read-only is really what I'm looking for, but for some mysterious reason it doesn't work.
我是否需要使用一些奇怪的技巧才能使只读模式按预期工作?我应该只用 JavaScript 来做吗?
Is there some weird trick I need to pull to get read-only to work as expected? Should I just do it in JavaScript instead?
顺便说一句,有谁知道为什么只读在单选按钮中不起作用,而在其他输入标签中起作用?这是 HTML 规范中那些难以理解的遗漏之一吗?
Incidentally, does anyone know why read-only 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?
推荐答案
我通过仅禁用未选中的单选按钮在单选按钮上伪造了只读.它可以防止用户选择不同的值,并且选中的值将始终在提交时发布.
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.
使用jQuery进行只读:
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.
这篇关于为什么单选按钮不能“只读"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!