LoginSignup
10
12

More than 5 years have passed since last update.

railsでviewのtext_fieldにdatalistを表示させる

Posted at

datalistについて

datalist 要素 - HTML | MDN

View

実装

  • text_fieldタグにlist:オプションを追加してdatalistのidを指定する
  • datalistタグを追加する
  • datalistの候補値をoptionとして指定する
xxx.html.erb
<%= form.text_field :name, id: :user_name, list: 'list-name' %>
<datalist id="list-name">
  <option value="akasaka">
  <option value="akatsuka">
  <option value="akabane">
</datalist>

動作

テキストフィールドが空欄の場合はすべての値が候補として表示される。

datalist2.png

テキストフィールドに値が入力されてる場合は一致する値のみ候補として表示される。

datalist3.png

10
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
12