以下代码将同级对象设为fieldsets
,以便在上一个中选择fieldsets
时可以启用这些input buttons
:
document.querySelectorAll("fieldset").forEach(fieldset => {
fieldset.addEventListener("change", function() {
let nextFieldset = this.nextElementSibling
while (nextFieldset && !nextFieldset.disabled) {
nextFieldset = nextFieldset.nextElementSibling
}
if (nextFieldset) {
nextFieldset.disabled = false
}
})
})
<form>
<fieldset> … </fieldset>
<fieldset> … </fieldset>
<fieldset> … </fieldset>
</form>
如何定位位于单独
fieldsets
中的sections
?<form>
<section><fieldset> … </fieldset></section>
<section><fieldset> … </fieldset></section>
<section><fieldset> … </fieldset></section>
</form>
最佳答案
这是完整的工作解决方案:
document.querySelectorAll("section").forEach(section => {
section.addEventListener("change", function() {
let nextFieldset = this.nextElementSibling.querySelector("fieldset")
while (nextFieldset && !nextFieldset.disabled) {
nextFieldset = nextFieldset.nextElementSibling
}
if (nextFieldset) {
nextFieldset.disabled = false
}
})
})