fieldset.disabled Freezes A Form While Submitting
Disable a whole form with one fieldset.disabled flag during submission, and release it in finally so a failed gateway never leaves the form locked.
Code
const form = document.querySelector("#checkout");
const fieldset = form.querySelector("fieldset");
const payButton = form.querySelector("button[type='submit']");
form.addEventListener("submit", async (event) => {
event.preventDefault();
// fieldset.disabled 一次冻结内部全部控件(含提交按钮)
fieldset.disabled = true;
payButton.textContent = "Paying…";
try {
await new Promise((resolve) => setTimeout(resolve, 300)); // 模拟网关
console.log("payment accepted");
form.reset();
} finally {
// 失败也要解锁:网关报错不能把表单永久锁死
fieldset.disabled = false;
payButton.textContent = "Pay now";
}
});
How it works
The async parts await their results, which keeps ordering predictable.
Module-level bindings hold the data — form, fieldset, payButton — with no function scope in between.
What to watch for
- No external packages — everything relies on the language’s built-in APIs.
- Uses
document, so it needs a browser context — it will not run as-is in Node without shims.
Using it
The demo output is labeled (payment accepted), so every printed line maps back to its originating operation.
Key statements
await new Promise((resolve) => setTimeout(resolve, 300)); // 模拟网关
console.log("payment accepted");
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
FAQ
- Does fieldset.disabled Freezes A Form While Submitting run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.