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.

JavascriptForm Validation

Code

javascriptsnippet
20 lines
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

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.