Why can#39;t radio buttons be quot;readonlyquot;?(为什么单选按钮不能“只读?)
问题描述
我想显示一个单选按钮,提交它的值,但根据具体情况,它是否不可编辑.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.
这篇关于为什么单选按钮不能“只读"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么单选按钮不能“只读"?


基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01