input
事件,该事件对任何表单元素也有效演示:
const myForm = document.forms['my-form'];
myForm.addEventListener('submit', e =>
{
e.preventDefault(); // disable submit - page reload
const formValues = Object.fromEntries(new FormData(myForm).entries());
console.log( formValues );
setInterval(console.clear,8000);
})
myForm.addEventListener('input', e =>
{
if (!e.target.matches('input[type="radio"]')) return;
console.log(myForm['difficulty-ask'].value);
setInterval(console.clear,2000);
});
label { display:block; }
fieldset { width: fit-content; }
<form name="my-form">
<fieldset>
<legend> difficulty </legend>
<label>
<input name="difficulty-ask" type="radio" value="all" checked >
All
</label>
<label>
<input name="difficulty-ask" type="radio" value="easy" >
Easy
</label>
<label>
<input name="difficulty-ask" type="radio" value="medium" >
Medium
</label>
<label>
<input name="difficulty-ask" type="radio" value="hard" >
Hard
</label>
</fieldset>
<button>submit</button>
</form>