如何使用 jquery 在 rails 上使用 ruby 的简单形式获取选定值

how use jquery to get selected value using simple form of ruby on rails

我无法从下拉列表中获取所选值的值

我的代码 jquery 是:$( "#statistic_commune option:selected" ).text();

当我只使用 html 时,此代码有效,例如:select.form-control name="statistic[state]" id="statistic_commune "

但当我使用时它不起作用:rails 标签如 = towns_select_tag(:statistic, :commune, {include_blank: true}, {class: 'form-control'},{id: 'statistic_commune'}) 请帮忙

您需要定位 select 标签:

$( "#statistic_commune" ).val();

你应该这样做

= towns_select_tag(:statistic, :commune, {include_blank: true}, {class: 'form-control'}, :id => "statistic_commune")

您应该通过使用获得价值:

$('#statistic_commune').val();