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

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

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


当前回答

我想出了一个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属性来启用/禁用单选按钮/复选框。

其他回答

I have a lengthy form (250+ fields) that posts to a db. It is an online employment application. When an admin goes to look at an application that has been filed, the form is populated with data from the db. Input texts and textareas are replaced with the text they submitted but the radios and checkboxes are useful to keep as form elements. Disabling them makes them harder to read. Setting the .checked property to false onclick won't work because they may have been checked by the user filling out the app. Anyhow...

onclick="return false;"

工作就像一个“禁用”无线电和复选框事实上的魅力。

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

我通过只禁用未选中的单选按钮来伪造单选按钮的只读。它可以防止用户选择不同的值,并且选中的值将始终在提交时发布。

使用jQuery使只读:

$(':radio:not(:checked)').attr('disabled', true);

这种方法也适用于将选择列表设置为只读,只是需要禁用每个未选择的选项。

在JS中捕获一个“On_click()”事件并像这里一样检查它怎么样?:

http://www.dynamicdrive.com/forums/showthread.php?t=33043

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

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