Javascript 如何使用顺序ID禁用多个单选按钮?
我需要禁用所有单选按钮;最好的方法是使用javascript,但我不太擅长;我尝试循环ID,但这是一场灾难! 我最终用6行代码一次禁用每个单选按钮;我能做得更有效一点吗Javascript 如何使用顺序ID禁用多个单选按钮?,javascript,html,Javascript,Html,我需要禁用所有单选按钮;最好的方法是使用javascript,但我不太擅长;我尝试循环ID,但这是一场灾难! 我最终用6行代码一次禁用每个单选按钮;我能做得更有效一点吗 document.getElementById(“radio1”).disabled=true; document.getElementById(“radio2”).disabled=true; document.getElementById(“radio3”).disabled=true; document.getEleme
document.getElementById(“radio1”).disabled=true;
document.getElementById(“radio2”).disabled=true;
document.getElementById(“radio3”).disabled=true;
document.getElementById(“radio4”).disabled=true;
document.getElementById(“radio5”).disabled=true;
document.getElementById(“radio6”).disabled=true代码>
单选按钮:
1
2
3
4
5
6
您可以使用querySelectorAll()
选择所有输入,然后使用loop在每个输入上设置disabled=true
var inputs=document.querySelectorAll('input[type=“radio”]”);
对于(变量i=0;i
单选按钮:
1
2
3
4
5
6
您可以使用querySelectorAll()
选择所有输入,然后使用loop在每个输入上设置disabled=true
var inputs=document.querySelectorAll('input[type=“radio”]”);
对于(变量i=0;i
单选按钮:
1
2
3
4
5
6
不知道为什么循环是一场灾难。我会这样做:
for (var i = 1; i <= 6; i++) {
document.getElementById("radio" + i).disabled = true;
}
不知道为什么循环是一场灾难。我会这样做:
for (var i = 1; i <= 6; i++) {
document.getElementById("radio" + i).disabled = true;
}
您可以使用css选择器(document.queryselectoral):
var radios=document.querySelectorAll(“[id^='radio']”)//获取id以“radio”开头的所有元素
对于(变量i=0;i
您可以使用css选择器(document.queryselectoral):
var radios=document.querySelectorAll(“[id^='radio']”)//获取id以“radio”开头的所有元素
对于(变量i=0;i
您可以按照这些思路做一些事情
// load all the inputs in the document into memory
var inputs = document.getElementsByTagName("input");
// loop over them, and set the disabled attribute on each of them
for (var i = 0; i < inputs.length; i++) {
inputs[i].disabled = true;
}
然后
var container = document.getElementById("input-container");
var inputs = container.getElementsByTagName("input");
循环保持不变您可以沿着这些路线做一些事情
// load all the inputs in the document into memory
var inputs = document.getElementsByTagName("input");
// loop over them, and set the disabled attribute on each of them
for (var i = 0; i < inputs.length; i++) {
inputs[i].disabled = true;
}
然后
var container = document.getElementById("input-container");
var inputs = container.getElementsByTagName("input");
循环保持不变这将禁用页面上的任何其他输入元素Yes fixed,因此现在它只会选择input[type=“radio”]
,这是很好的选择;以防万一,我必须添加更多的输入请求!这将禁用页面上的任何其他输入元素Yes fixed,因此现在它只会选择input[type=“radio”]
,这是很好的选择;以防万一,我必须添加更多的输入请求!