5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsのselect_tagで選択時のイベントを検知して値を拾う

Last updated at Posted at 2015-11-13

Rails初心者の私が試行錯誤しながら簡単なツールを開発中。
何から何まで分からないので、色んな所で詰まる。まず初歩の初歩でつまずいたこと。

select_tag を使ったときに、選択が変更された時のイベントと値を拾いたかった。

とりあえず以下のコードで拾うことが出来た。

erbのファイルが以下

<%= form_tag(:name, :Label) %>
  <%= select_tag(:selected_value, options_for_select(['aaa', 'bbb', 'ccc'], class: 'select_action') %>
<% end %>

続いてCoffeeScript

$ ->
  do ->
    onChangeEvent= ->
      value = $(".select-action option:selected").text();
      alert(value)
 
    $('.select_action').on
      'change': onChangeEvent

これで、選択時のイベントとともにValueが取れるようになった。

5
7
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
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?