gpt4 book ai didi

javascript - 禁用字段集无法按预期工作

转载 作者:行者123 更新时间:2023-11-30 10:58:32 24 4
gpt4 key购买 nike

在我的演示表单中,字段集在选择按钮时按顺序启用。因此,选中第一个字段集中的按钮会启用第二个,依此类推。

如果第一个字段集中的所有复选框都未选中,则应重置表单,然后应再次禁用第二至第四个字段集。但事实并非如此。第二个字段集以某种方式保持启用状态。这里有什么问题?

document.addEventListener("DOMContentLoaded", _ => {
const form = document.forms[0]

// First, disable all fieldsets except the first

function disableFieldsets() {
const disabledFieldsets = form.querySelectorAll(
"section:not(:first-of-type) fieldset"
)

for (let i = 0; i < disabledFieldsets.length; i++) {
disabledFieldsets[i].disabled = true
}
}

disableFieldsets()

// Sequentially enable fieldsets on selection

const sections = form.querySelectorAll("section")

sections.forEach(section => {
section.addEventListener("change", function() {
let nextFieldset = this.nextElementSibling.querySelector("fieldset")

if (nextFieldset) {
nextFieldset.disabled = false
}
})
})

// Reset form and disable fieldsets after all inputs in the first fieldset are deselected

const firstFieldsetButtons = form.querySelectorAll("[name=First]")
let isChecked = false

firstFieldsetButtons.forEach(firstFieldsetButton => {
firstFieldsetButton.addEventListener("click", function(e) {
if (this.checked) {
isChecked = true
} else {
form.reset()
disableFieldsets()
}
})
})
})
<form method=post action=submit>    
<section>
<fieldset>
<legend>First Fieldset</legend>
<label><input type=checkbox name=First value=A>A</label>
<label><input type=checkbox name=First value=B>B</label>
<label><input type=checkbox name=First value=C>C</label>
</fieldset>
</section>

<section>
<fieldset>
<legend>Second Fieldset</legend>
<label><input type=radio name=Second value=1.1>1.1</label>
<label><input type=radio name=Second value=1.2>1.2</label>
<label><input type=radio name=Second value=1.3>1.3</label>
</fieldset>
</section>

<section>
<fieldset>
<legend>Third Fieldset</legend>
<label><input type=radio name=Third value=2.1>2.1</label>
<label><input type=radio name=Third value=2.2>2.2</label>
<label><input type=radio name=Third value=2.3>2.3</label>
</fieldset>
</section>

<section>
<fieldset>
<legend>Fourth Fieldset</legend>
<label><input type=radio name=Fourth value=3.1>3.1</label>
<label><input type=radio name=Fourth value=3.2>3.2</label>
<label><input type=radio name=Fourth value=3.3>3.3</label>
</fieldset>
</section>

<input type=submit value=Submit>
</form>

最佳答案

每次更改第一个字段集时都会启用第二个字段集 - 即使您取消选中复选框也是如此。即使您在“单击”复选框后禁用了字段集,您的 “change” 事件也会在此事件之后运行,从而启用第二个字段集。这可以通过使用标志来指示表单是否已重置来解决:

document.addEventListener("DOMContentLoaded", _ => {
const form = document.forms[0];

// First, disable all fieldsets except the first

function disableFieldsets() {
const disabledFieldsets = form.querySelectorAll(
"section:not(:first-of-type) fieldset"
)

for (let i = 0; i < disabledFieldsets.length; i++) {
disabledFieldsets[i].disabled = true
}
}

disableFieldsets()

// Sequentially enable fieldsets on selection

const sections = form.querySelectorAll("section")
let reset = false;
sections.forEach(section => {
section.addEventListener("change", function() {
let nextFieldset = this.nextElementSibling.querySelector("fieldset")
if (nextFieldset && !reset) {
nextFieldset.disabled = false;
} else if(reset) {
reset = false;
}
})
})

// Reset form and disable fieldsets after all inputs in the first fieldset are deselected

const firstFieldsetButtons = form.querySelectorAll("[name=First]")
let isChecked = false;

firstFieldsetButtons.forEach(firstFieldsetButton => {
firstFieldsetButton.addEventListener("click", function(e) {
if (this.checked) {
isChecked = true;
} else {
form.reset();
disableFieldsets();
reset = true;
}
})
})
})
<form method=post action=submit>    
<section>
<fieldset>
<legend>First Fieldset</legend>
<label><input type=checkbox name=First value=A>A</label>
<label><input type=checkbox name=First value=B>B</label>
<label><input type=checkbox name=First value=C>C</label>
</fieldset>
</section>

<section>
<fieldset>
<legend>Second Fieldset</legend>
<label><input type=radio name=Second value=1.1>1.1</label>
<label><input type=radio name=Second value=1.2>1.2</label>
<label><input type=radio name=Second value=1.3>1.3</label>
</fieldset>
</section>

<section>
<fieldset>
<legend>Third Fieldset</legend>
<label><input type=radio name=Third value=2.1>2.1</label>
<label><input type=radio name=Third value=2.2>2.2</label>
<label><input type=radio name=Third value=2.3>2.3</label>
</fieldset>
</section>

<section>
<fieldset>
<legend>Fourth Fieldset</legend>
<label><input type=radio name=Fourth value=3.1>3.1</label>
<label><input type=radio name=Fourth value=3.2>3.2</label>
<label><input type=radio name=Fourth value=3.3>3.3</label>
</fieldset>
</section>

<input type=submit value=Submit>
</form>

关于javascript - 禁用字段集无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59027088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com