我想显示一个单选按钮,有它的值提交,但根据情况,它是不可编辑的。Disabled不起作用,因为它不提交值(或者它是否提交?),并且它使单选按钮变灰。只读是我真正想要的,但出于某种神秘的原因,它不起作用。
我是否需要使用一些奇怪的技巧来实现只读以达到预期的效果?我应该用JavaScript写吗?
顺便提一下,有人知道为什么只读在单选按钮中不起作用,而在其他输入标签中却起作用吗?这是HTML规范中不可理解的遗漏之一吗?
我想显示一个单选按钮,有它的值提交,但根据情况,它是不可编辑的。Disabled不起作用,因为它不提交值(或者它是否提交?),并且它使单选按钮变灰。只读是我真正想要的,但出于某种神秘的原因,它不起作用。
我是否需要使用一些奇怪的技巧来实现只读以达到预期的效果?我应该用JavaScript写吗?
顺便提一下,有人知道为什么只读在单选按钮中不起作用,而在其他输入标签中却起作用吗?这是HTML规范中不可理解的遗漏之一吗?
当前回答
JavaScript的方式-这对我很有用。
<script>
$(document).ready(function() {
$('#YourTableId').find('*').each(function () { $(this).attr("disabled", true); });
});
</script>
原因:
$('#YourTableId').find('*') ->返回所有标签。 $ (' # YourTableId ');(“*”)。Each (function () {$(this)。attr(“禁用”,真正的);}); 遍历在此捕获的所有对象并禁用输入标记。
分析(调试):
Form:radiobutton在内部被认为是一个“输入”标签。 像上面的函数()一样,如果你尝试打印document.write(this.tagName); 只要在标签中找到单选按钮,它就返回一个输入标签。
因此,上面的代码行可以更优化的单选按钮标签,通过输入替换*: $ (' # YourTableId ');(“输入”)。Each (function () {$(this)。attr(“禁用”,真正的);});
其他回答
这是我的解决方案(覆盖)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;
}
}
});
一个相当简单的选择是创建一个javascript函数,在表单的“onsubmit”事件上调用,以启用单选按钮返回,这样它的值就会与表单的其余部分一起发布。 这似乎不是HTML规范上的一个遗漏,而是一个设计选择(一个逻辑选择,IMHO),单选按钮不能像按钮一样是只读的,如果你不想使用它,那就禁用它。
在JS中捕获一个“On_click()”事件并像这里一样检查它怎么样?:
http://www.dynamicdrive.com/forums/showthread.php?t=33043
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;"
工作就像一个“禁用”无线电和复选框事实上的魅力。
最好的解决方案是设置已检查或未检查状态(无论是从客户端还是服务器),并且不让用户更改它之后(即使其为只读)执行以下操作:
<input type="radio" name="name" onclick="javascript: return false;" />