我想在特定时间禁用某组按钮。使用这个js:
document.getElementById("reset").setAttribute('disabled','disabled');
但是所有的ID都不一样,按钮就像<button id=1> <button id=2>
我可以设置一些虚构的变量,例如 <button group="hello">
然后做这样的事情:
document.getElementById("group:hello").setAttribute('disabled','disabled');
要禁用所有按钮,即使按钮之间的 ID 属性不同?
最佳答案
您可以使用 data 属性,请参阅下一步:
<button data-group="hello" id="button1">test1</button>
<button data-group="hello" id="button2">test2</button>
<button data-group="hello" id="button3">test3</button>
和 JS
document.querySelectorAll('[data-group="hello"]').forEach(function(button) {
button.disabled = true;
});
编辑,这是 fiddle https://jsfiddle.net/xycmj4gv/2/
关于javascript - 当 id 不同时禁用某些按钮 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49365970/