为什么单选按钮不能“只读”? [英] Why can't radio buttons be "readonly"?

查看:169
本文介绍了为什么单选按钮不能“只读”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个单选按钮,提交它的值,但根据具体情况,不能编辑它。禁用不起作用,因为它没有提交值(或者不是?),并且它会使单选按钮变为灰色。只读是我真正想要的,但出于某种神秘原因,它不起作用。

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);

这种方法也适用于只读选择列表,除了您需要禁用每个un - 选定的选项。

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

这篇关于为什么单选按钮不能“只读”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆