【问题标题】:Disable many fields by name javascript按名称 javascript 禁用许多字段
【发布时间】:2018-12-29 05:06:09
【问题描述】:
这是我的问题:
我有一个jsp页面,这个jsp有很多这样的文本字段:
<html:text property="cicPF" maxlength="9" style="text-transform: uppercase;" onfocus="disableIfeFields()"/>
所以我想在焦点位于特定字段时禁用其中一些文本字段
但是这些字段都没有“id”标签,我无法修改它以包含它。
我可以禁用使用它们给定名称的字段吗,没有一个重复相同的名称。
例如使用这样的函数:
function disableIfeFields(){
document.getElementsByName("numIdentificacionPF").disabled = true;
}
谢谢
【问题讨论】:
标签:
javascript
function
jsp
disabled-input
【解决方案1】:
您需要遍历列表并禁用所有您想要的字段,使用输入来显示示例:
function disableIfeFields() {
document.getElementsByName("numIdentificacionPF").forEach((e) => {
e.disabled = true;
});
}
<html:text property="cicPF" maxlength="9" style="text-transform: uppercase;" />
<input onfocus="disableIfeFields()" type="text" name="fname">
<input type="text" name="numIdentificacionPF">
<input type="text" name="numIdentificacionPF">
<input type="text" name="numIdentificacionPF">
<input type="text" name="numIdentificacionPF">
<input type="text" name="numIdentificacionPF">
【解决方案2】:
希望以下内容对您有所帮助。因为选择结果是一个元素列表,所以您必须遍历结果。
请注意,由于您说没有输入重复相同的名称,所以我使用querySelectorAll,毕竟这可能是一个更合适的方法......
var inputs = document.querySelectorAll('input[type="text"]');
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].id === 'label') {
continue;
}
inputs[i].disabled = true;
}
【解决方案3】:
可能是这样的:
function disableIfeFields(){
document.querySelectorAll('[property="cicPF"]')[0].disabled = true;
}
disableIfeFields();
<input type="text" property="cicPF" maxlength="9" style="text-transform: uppercase;" onfocus="disableIfeFields()"/>