【Rails】セレクトタグの選択肢は確認できるが、選択できないようにする。
select_tag
ドキュメント
選択ボックスを生成
options_for_select
ドキュメント
配列・ハッシュから選択肢を生成する
今回作成したいHTML
<select name="select" id="select">
<option selected="selected" disabled="disabled" value="blank">選択 ▼</option>
<option disabled="disabled" value="value1">key1</option>
<option disabled="disabled" value="value2">key2</option>
</select>
<select name="select" id="select">
<option selected="selected" disabled="disabled" value="blank">選択 ▼</option>
<option disabled="disabled" value="value1">key1</option>
<option disabled="disabled" value="value2">key2</option>
</select>
⇦選択ボックスがあって
⇦選択肢は見れるけど選択はできない
別に全部選択できないようにする必要がないって場合でも
ちょっと手を加えれば一部だけ選択ができないとかでもできる。
slimで書く
erbでもいいんだけど最近slimで書く機会が多くてね...
- choices = [['選択 ▼','blank'],['key1','value1'],['key2','value2']]
= select_tag :select,
options_for_select(choices, selected: 'blank', disabled: choices.map(&:last))
ドキュメントには:selected
が指定できるのは書いてあるけど
:disabled
が指定できるのは書いてない。
disabled="disabled"
にしたい項目のvalueを配列で指定すると
その項目は選択することができなくなる。
ちなみに
- choices = [['選択 ▼','blank'],['key1','value1'],['key2','value2']]
= select_tag :select,
options_for_select(choices, selected: 'blank'),
disabled: true
- choices = [['選択 ▼','blank'],['key1','value1'],['key2','value2']]
= select_tag :select,
options_for_select(choices, selected: 'blank'),
disabled: true
とすると
セレクトボックスそのものが無効になり、選択肢を確認することすら不可能になる。
(厳密には開発者ツール使えば見れるけど)
終わり。
Author And Source
この問題について(【Rails】セレクトタグの選択肢は確認できるが、選択できないようにする。), 我々は、より多くの情報をここで見つけました https://qiita.com/rhiroe/items/6687f7fb7af41409fcf2著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .