我希望展示一个单选按钮,并将其值提交,但根据情况,它不可编辑。禁用不起作用,因为它不会提交值(或者会吗?),且灰显了单选按钮。实际上,只读是我正在寻找的,但由于某种神秘原因,它不起作用。
有没有一些奇怪的技巧可以使只读功能按预期工作?我是否应该在JavaScript中完成它?
顺便问一下,有人知道为什么只读无法在单选钮中工作,而在其他输入标记中可以工作吗?这是HTML规范中那些难以理解的遗漏之一吗?
我希望展示一个单选按钮,并将其值提交,但根据情况,它不可编辑。禁用不起作用,因为它不会提交值(或者会吗?),且灰显了单选按钮。实际上,只读是我正在寻找的,但由于某种神秘原因,它不起作用。
有没有一些奇怪的技巧可以使只读功能按预期工作?我是否应该在JavaScript中完成它?
顺便问一下,有人知道为什么只读无法在单选钮中工作,而在其他输入标记中可以工作吗?这是HTML规范中那些难以理解的遗漏之一吗?
如果有其他选项,单选按钮只需要是只读的。如果没有其他选项,则已选中的单选按钮无法取消选中。如果您有其他选项,可以通过禁用其他选项来阻止用户仅更改值:
<input type="radio" name="foo" value="Y" checked>
<input type="radio" name="foo" value="N" disabled>
Fiddle: http://jsfiddle.net/qqVGu/
onclick="return false"
来防止更改。 - dmitry我曾通过禁用未选中的单选按钮来模拟单选按钮的只读属性。这样可以防止用户选择不同的值,并且在提交时,所选的值将始终被提交。
使用jQuery实现只读:
$(':radio:not(:checked)').attr('disabled', true);
这种方法也可以用于使选择列表只读,但是您需要禁用每个未选中的选项。
$(':radio[readonly]:not(:checked)').attr('disabled', true);
- wodow这是一个可以使用的技巧。
<input type="radio" name="name" onclick="this.checked = false;" />
onClick="this.checked = <%=value%>"
:) - JustLorenonclick="return false;"
来保持组的值不变。 - Zach我有一个很长的表单(超过250个字段)要提交到数据库。这是一个在线就业申请表。当管理员查看已提交的申请时,表单会填充来自数据库的数据。输入文本和文本区域将替换为用户提交的文本,但单选框和复选框需要保留为表单元素。禁用它们会使它们更难读取。在单击时将.checked属性设置为false不起作用,因为它们可能已由填写应用程序的用户选择。总之...
onclick="return false;"
对于“禁用”单选框和复选框,它的效果非常好。
最佳解决方案是设置选中或未选中状态(可以来自客户端或服务器),并且不让用户在之后更改它(即将其设置为只读)。请执行以下操作:
<input type="radio" name="name" onclick="javascript: return false;" />
<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>
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属性来启用/禁用单选按钮/复选框。
对于未被选中的单选按钮,请将它们标记为禁用状态。这可以防止它们响应用户输入并清除已选中的单选按钮。例如:
<input type="radio" name="var" checked="yes" value="Yes"></input>
<input type="radio" name="var" disabled="yes" value="No"></input>
checked="yes"
是什么规范?建议使用checked="checked"
或者只使用属性checked
而不带值。对于disabled
属性也是同样的情况。 - Robin van BaalenJavaScript方法 - 这对我有用。
<script>
$(document).ready(function() {
$('#YourTableId').find('*').each(function () { $(this).attr("disabled", true); });
});
</script>
原因:
$('#YourTableId').find('*')
-> 这将返回所有标签。
$('#YourTableId').find('*').each(function () { $(this).attr("disabled", true); });
遍历此中捕获的所有对象并禁用输入标签。
分析(调试):
form:radiobutton
在内部被视为 "input" 标签。
像上面的函数()一样,如果您尝试打印 document.write(this.tagName);
无论在哪个标签中找到单选按钮,它都会返回一个输入标签。
因此,对于单选按钮标记,上述代码行可以通过将 * 替换为 input 来进行更优化:
$('#YourTableId').find('input').each(function () { $(this).attr("disabled", true); });
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 的情况下都能正常工作。
disabled
,但我认为您将无法获取单选按钮的值。
或者可以设置图像,例如:<img src="itischecked.gif" alt="[x]" />radio button option
此致敬礼。