如何使用 jquery rails 获取 html select 的下拉值?

How do I get the dropdown value of the html select using jquery rails?

HAML 文件:

 = form_for '', :remote => true do |f|
   = f.select :stp_name, @steps

JS:

  title = $('#stp_id').val()
  console.log title

控制台总是returns"undefined"。我尝试了其他答案的解决方案,但其中 none 似乎 work.Thanks!

您可以将标识符添加到您的 select 标签、id 或 class,然后使用 jQuery 使用 val 来获取所选值。注意将 id 添加到 Rails select 助手的方法,html_options 必须作为第四个参数:

= f.select :stp_name, @steps, {}, id: 'select'

然后$('#select').val()会给你值。