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

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

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


当前回答

这是你可以使用的技巧。

<input type="radio" name="name" onclick="this.checked = false;" />

其他回答

我发现使用onclick='this。Checked = false;'在一定程度上起了作用。单击的单选按钮将不会被选中。然而,如果有一个单选按钮已经被选中(例如,一个默认值),该单选按钮将变为未选中。

<!-- didn't completely work -->
<input type="radio" name="r1" id="r1" value="N" checked="checked" onclick='this.checked = false;'>N</input>
<input type="radio" name="r1" id="r1" value="Y" onclick='this.checked = false;'>Y</input>

在这种情况下,保持默认值不变并禁用其他单选按钮将保留已经选中的单选按钮,并防止不选中它。

<!-- preserves pre-selected value -->
<input type="radio" name="r1" id="r1" value="N" checked="checked">N</input>
<input type="radio" name="r1" id="r1" value="Y" disabled>Y</input>

这个解决方案并不是防止默认值被更改的最优雅的方法,但无论是否启用javascript,它都是有效的。

这是你可以使用的技巧。

<input type="radio" name="name" onclick="this.checked = false;" />

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

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

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

我想出了一个javascript的方法来实现复选框和单选按钮的只读状态。它针对当前版本的Firefox, Opera, Safari,谷歌Chrome,以及当前和以前版本的IE(直到IE7)进行了测试。

为什么不简单地使用disabled属性呢?打印页面时,禁用的输入元素显示为灰色。实现此功能的客户希望所有元素都是相同的颜色。

我不确定我是否可以在这里发布源代码,因为我是在为一家公司工作时开发的,但我肯定可以分享这些概念。

使用onmousedown事件,您可以在单击操作更改选择状态之前读取它。因此,您存储这些信息,然后使用onclick事件恢复这些状态。

<input id="r1" type="radio" name="group1" value="r1" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);" checked="checked">Option 1</input>
<input id="r2" type="radio" name="group1" value="r2" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);">Option 2</input>
<input id="r3" type="radio" name="group1" value="r3" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);">Option 3</input>

<input id="c1" type="checkbox" name="group2" value="c1" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);" checked="checked">Option 1</input>
<input id="c2" type="checkbox" name="group2" value="c2" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);">Option 2</input>
<input id="c3" type="checkbox" name="group2" value="c3" onmousedown="storeSelectedRadiosForThisGroup(this.name);" onclick="setSelectedStateForEachElementOfThisGroup(this.name);" checked="checked">Option 3</input>

javascript部分将像这样工作(同样只是概念):

var selectionStore = new Object();  // keep the currently selected items' ids in a store

function storeSelectedRadiosForThisGroup(elementName) {
    // get all the elements for this group
    var radioOrSelectGroup = document.getElementsByName(elementName);

    // iterate over the group to find the selected values and store the selected ids in the selectionStore
    // ((radioOrSelectGroup[i].checked == true) tells you that)
    // remember checkbox groups can have multiple checked items, so you you might need an array for the ids
    ...
}

function setSelectedStateForEachElementOfThisGroup(elementName) {
    // iterate over the group and set the elements checked property to true/false, depending on whether their id is in the selectionStore
    ...

    // make sure you return false here
    return false;
}

您现在可以通过更改输入元素的onclick和onmousedown属性来启用/禁用单选按钮/复选框。

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