Help us understand the problem. What is going on with this article?

【小ネタ】 HTML のチェックボックスを readonly にする

More than 5 years have passed since last update.

HTML のチェックボックスには何故か readonly 属性がないらしい。ただし HTML のチェックボックスは OFF 時には項目そのものが送られないため、普通は hidden タイプの要素を組み合わせる。

<input type="hidden" name="checkItem1" value="0">
<input type="checkbox" onclick="this.form.checkItem.value=this.checked ? 1 : 0" id='normal'> <label for='normal'>通常のチェックボックス</label>


通常のチェックボックス

したがって、チェックボックスを readonly にするには disabled 属性を使って

<input type="hidden" name="checkItem2" value="0">
<input type="checkbox" id='checkoff' disabled='disabled'> <label for='checkoff'>チェック OFF の状態で固定</label>
<input type="hidden" name="checkItem3" value="1">
<input type="checkbox" id='checkon' disabled='disabled' checked='checked'> <label for='checkon'>チェック ON の状態で固定</label>


チェック OFF の状態で固定

チェック ON の状態で固定

とすればいいようだ。

spiegel-im-spiegel
職業エンジニアは無期休業中。
https://baldanders.info/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした