日本語

React readOnly vs disabled: Which Input Values Get Submitted

​​React​Forms,HTML,Playwright,TypeScript

If you add readOnly to a React input, users cannot change the value, but the value is still sent with the form. If you add disabled, users cannot change the value and it is not sent at all. When you want to show a member ID on an edit screen without letting users change it but still send it to the server, or show a plan that cannot be selected as grayed out in a signup form, the submitted value tells you which attribute to use.

In my test environment, I opened a form with a readOnly field and a disabled field side by side in Chromium. I typed into each field, submitted the form, and recorded the body the server received.

Note

The test environment was Ubuntu 24.04 and Node.js 22.23.4. I used react / react-dom 19.3.1, @types/react / @types/react-dom 19.3.0, typescript 7.0.2, esbuild 0.28.2, and Playwright 1.63.0 with its bundled Chromium 153. I type-checked the .tsx files with tsc, bundled them with esbuild, and served them from a small Node.js server. Keyboard input and clicks were done with Playwright. I did not test Firefox, Safari, mobile browsers, <textarea> and <select>, server-side validation of the values, or a readOnly attribute on a checkbox controlled by checked.

readOnly values are sent, disabled values are not

I compared the following form. It uses method="post" and submits as a normal HTML form. Initial values were passed with defaultValue.

tsx
export function ProfileForm() {
  return (
    <form method="post" action="/submit">
      <label>
        会員番号(readOnly)
        <input name="memberId" defaultValue="A-1024" readOnly />
      </label>
      <label>
        プラン(disabled)
        <input name="plan" defaultValue="スタンダード" disabled />
      </label>
      <label>
        名前
        <input name="name" defaultValue="" />
      </label>
      <button type="submit">送信</button>
    </form>
  );
}

The test server returns the body it receives at /submit as is. (The second line of the record below is that body rewritten as name-value pairs.) I clicked the member ID and plan fields and typed into them, entered the name Nekota in the name field, and then submitted.

text
最初: memberId="A-1024"  plan="スタンダード"  name=""
readOnly の欄をクリックしたあとのフォーカス: memberId
disabled の欄をクリックしたあとのフォーカス: BODY
入力を試したあと: memberId="A-1024"  plan="スタンダード"  name="猫田"
(中略)
サーバーに届いた本文: memberId=A-1024&name=%E7%8C%AB%E7%94%B0
["memberId=A-1024","name=猫田"]

Typing did not change the value in either field. The difference shows up on submit. The server received only memberId and name. plan was not in the body at all. It was not sent as an empty value; the field was dropped entirely.

The following GIF shows the same form operated in the browser. The keys I pressed are shown at the top right, and the values the server received are shown in the box below. In the GIF, I typed "neko" into the name field.

Clicking the member ID (readOnly) and plan (disabled) fields, typing XYZ, typing neko into the name field, and submitting. The box below shows memberId = A-1024 and name = neko, and plan is shown in pink as "not received" (labels in the image are in Japanese)
The values the server received after typing into the readOnly and disabled fields and submitting

When I clicked the member ID field, a light blue outline appeared, which means it had focus. Clicking the plan field did not give it focus, and it stayed gray.

The MDN description of the readonly attribute also says that a readonly field can receive focus, while a disabled field cannot and is not submitted with the form.

Point

If a value should not be changed on screen but should still be sent to the server, use readOnly. A value in a disabled field does not reach the server.

Passing a function to form action gives the same result

In React 19, when you pass a function to the action prop of <form>, the function receives a FormData on submit. I placed the same three fields and displayed what the function received.

tsx
import { useState } from 'react';

export function ActionForm() {
  const [received, setReceived] = useState('');

  function submit(formData: FormData) {
    setReceived(JSON.stringify([...formData.entries()]));
  }

  return (
    <form action={submit}>
      <input name="memberId" defaultValue="A-1024" readOnly />
      <input name="plan" defaultValue="スタンダード" disabled />
      <input name="name" defaultValue="" />
      <button type="submit">送信</button>
      <output>{received}</output>
    </form>
  );
}

I entered the name Nekota in the name field and submitted. The result was:

text
action が受け取った FormData: [["memberId","A-1024"],["name","猫田"]]

Again, plan is missing. The FormData passed to the action function does not include the value of the disabled field.

Differences in focus and CSS

Besides submission, there are differences in how the fields behave. On a freshly opened page, I pressed the Tab key three times, and focus moved in this order:

text
開いたページで Tab を3回押したときのフォーカスの順: memberId → name → 送信

Focus stops at the readOnly member ID and skips the disabled plan. If you want keyboard-only users to reach the member ID field, readOnly is the better choice.

There is one caveat with CSS pseudo-classes:

text
:read-only に当たる欄: memberId, plan
:disabled に当たる欄: plan

:read-only matched the disabled field as well as the readOnly field. As the MDN description of :read-only says, :read-only matches every element that the user cannot edit. If you want to style only the readOnly field, selecting with :read-only alone will also affect the disabled field.

Here is a summary of the differences so far:

readOnly disabled
User can change the value No No
Focus and Tab navigation Yes No
Value sent on submit Included Not included
Matches :read-only Yes Yes
Appearance in the GIF above Same as a normal field Gray

Passing only value: the warning goes away with readOnly

If you pass a state value to value without an onChange handler, React's development build logs an error to the console. I covered this error itself in the article on the difference between controlled and uncontrolled inputs in React. Here I compare what happens when readOnly or disabled is added.

tsx
import { useState } from 'react';

export function NoOnChange() {
  const [memberId] = useState('A-1024');
  return <input name="memberId" value={memberId} />;
}

export function WithReadOnly() {
  const [memberId] = useState('A-1024');
  return <input name="memberId" value={memberId} readOnly />;
}

export function WithDisabled() {
  const [memberId] = useState('A-1024');
  return <input name="memberId" value={memberId} disabled />;
}
text
--- NoOnChange ---
文字を打ったあと: memberId="A-1024"
[console.error] You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.

--- WithReadOnly ---
文字を打ったあと: memberId="A-1024"
(コンソールのエラー・警告なし)

--- WithDisabled ---
文字を打ったあと: memberId="A-1024"
(コンソールのエラー・警告なし)

Without onChange, the value did not change when I typed, and the error was logged. Adding readOnly removed the error. The official reference on inputs that do not update when typed into also says that if you want the field to be read-only on purpose, add readOnly to get rid of the error. disabled also removed the error, but as noted above, a disabled field is not submitted.

readOnly does not work on a checkbox with defaultChecked

It is easy to assume that readOnly works on checkboxes the same way it does on text inputs, but it does not. I tested three checkboxes that were checked with defaultChecked.

tsx
export function CheckboxForm() {
  return (
    <form method="post" action="/submit">
      <label>
        <input type="checkbox" name="newsletter" value="yes" defaultChecked readOnly />
        お知らせメール(readOnly)
      </label>
      <label>
        <input type="checkbox" name="terms" value="agreed" defaultChecked disabled />
        利用規約に同意(disabled)
      </label>
      <label>
        <input type="checkbox" name="terms2" value="agreed" defaultChecked disabled />
        <input type="hidden" name="terms2" value="agreed" />
        利用規約に同意(disabled + hidden)
      </label>
      <button type="submit">送信</button>
    </form>
  );
}

I clicked the readOnly and disabled checkboxes and then submitted.

text
最初: newsletter=true  terms=true  terms2=true
readOnly と disabled のチェックボックスをクリックしたあと: newsletter=false  terms=true  terms2=true
サーバーに届いた本文: terms2=agreed

The readOnly checkbox was unchecked when I clicked it, so newsletter was not sent. As the MDN description of the readonly attribute says, readonly applies only to fields where text is entered. It has no effect on checkboxes or radio buttons.

The disabled checkbox could not be changed, but even though it was checked, terms was not sent. If you want to keep a value fixed and still send it, add a type="hidden" input with the same name and value, as in the third example. The disabled checkbox's value did not reach the server, and only terms2=agreed from the hidden input was received.

required does not take effect on readOnly fields

Finally, I added required to empty fields and pressed submit.

tsx
export function RequiredForm() {
  return (
    <form method="post" action="/submit">
      <input name="normal" defaultValue="" required />
      <input name="readonlyEmpty" defaultValue="" required readOnly />
      <input name="disabledEmpty" defaultValue="" required disabled />
      <button type="submit">送信</button>
    </form>
  );
}
text
willValidate: normal=true  readonlyEmpty=false  disabledEmpty=false
空のまま送信を押したあとの URL: /  フォーカス: normal
normal の validationMessage: Please fill out this field.
normal だけ埋めて送信したとき、サーバーに届いた本文: normal=abc&readonlyEmpty=

When the normal field was empty, submission was blocked and focus moved to that field. When I filled in only the normal field, the form was submitted even though the readOnly and disabled fields were still empty. Because willValidate is false, both fields are excluded from browser validation. The MDN description of the readonly attribute says that required has no effect on readonly fields, and that you should not put required on a readonly field in the first place. I added it in the code above only to check how the browser behaves.

Looking at the body received, the empty readOnly field was not stopped by browser validation and arrived as readonlyEmpty= with an empty value. The disabled field was not received at all, name included.

​Posted by 86