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

我是否需要使用一些奇怪的技巧来实现只读以达到预期的效果?我应该用JavaScript写吗?

顺便提一下,有人知道为什么只读在单选按钮中不起作用,而在其他输入标签中却起作用吗?这是HTML规范中不可理解的遗漏之一吗?


当前回答

一个相当简单的选择是创建一个javascript函数,在表单的“onsubmit”事件上调用,以启用单选按钮返回,这样它的值就会与表单的其余部分一起发布。 这似乎不是HTML规范上的一个遗漏,而是一个设计选择(一个逻辑选择,IMHO),单选按钮不能像按钮一样是只读的,如果你不想使用它,那就禁用它。

其他回答

这是我的解决方案(覆盖)Sencha ExtJS 7.2+(复选框和单选覆盖)


Ext.define('MyApp.override.field.Checkbox', {
    override: 'Ext.field.Checkbox',

    /**
     * OVERRIDE: to allow updateReadOnly to work propperly
     * @param {boolean} newValue
     *
     * To ensure the disabled state stays active if the field is still readOnly
     * we re - set the disabled state
     */
    updateDisabled(newValue) {
        this.callParent(arguments);

        if (!newValue && this.getReadOnly()) {
            this.inputElement.dom.disabled = true;
        }
    },

    /**
     * OVERRIDE: readonly for radiogroups and checkboxgroup do not work as other fields
     *     https://stackoverflow.com/questions/1953017/why-cant-radio-buttons-be-readonly
     *
     * @param {boolean} newValue
     *
     *  - use disabled on the field
     */
    updateReadOnly(value) {
        this.callParent(arguments);

        if (!this.getDisabled()) {
            this.inputElement.dom.disabled = value;
        }
    }
});

对于未选中的单选按钮,将它们标记为禁用。这将阻止它们响应用户输入并清除选中的单选按钮。例如:

<input type="radio" name="var" checked="yes" value="Yes"></input>
<input type="radio" name="var" disabled="yes" value="No"></input>

只有在有其他选项时,单选按钮才需要是只读的。如果没有其他选项,则无法取消选中的单选按钮。如果你有其他选项,你可以通过禁用其他选项来阻止用户更改值:

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

小提琴:http://jsfiddle.net/qqVGu/

我正在使用一个JS插件,样式复选框/单选输入元素,并使用以下jQuery来建立一个“只读状态”,其中底层值仍然发布,但输入元素似乎无法访问用户,这是我相信我们将使用只读输入属性的预期原因…

if ($(node).prop('readonly')) {
    $(node).parent('div').addClass('disabled'); // just styling, appears greyed out
    $(node).on('click', function (e) {
        e.preventDefault();
    });
}

摘自https://stackoverflow.com/a/71086058/18183749

如果你不能使用'disabled'属性(因为它会擦除值的 input at POST),并注意到html属性'readonly'只工作 在文本区域和一些输入(文本,密码,搜索,据我所见), 最后,如果你不想重复你所有的 选择,复选框和带有隐藏输入的单选,您可能会找到 以下功能或任何他的内部逻辑,你喜欢:

addReadOnlyToFormElements = function (idElement) {
    // html readonly don't work on input of type checkbox and radio, neither on select. So, a safe trick is to disable the non-selected items
    $('#' + idElement + ' input[type="radio"]:not(:checked)').prop('disabled',true); 

    // and, on the selected ones, mimic readonly appearance
    $('#' + idElement + ' input[type="radio"]:checked').css('opacity','0.5');
}

没有什么比删除这些只读更容易的了

removeReadOnlyFromFormElements = function (idElement) {
    // Remove the disabled attribut on non-selected
    $('#' + idElement + ' input[type="radio"]:not(:checked)').prop('disabled',false); 
 
    // Remove readonly appearance on selected ones
    $('#' + idElement + ' input[type="radio"]:checked').css('opacity','');
}