Rails应用程序中的jQuery不会读取包含空格的值。如何让它解释这些值?
我正在尝试使用jQuery自动更新Rails应用程序中的相关选择框。它适用于没有空格的值,如“Barahona”或“Manzanillo”,但当我尝试“Puerto Plata”或“Santo Domingo”时,它失败了 这里是当我选择“Puerto Plata”时控制台出现的错误 我使用的jQuery是Railscasts#88中采用的,并在此处列出,用于选择一个港口,然后过滤泊位,以仅列出该港口的泊位 以下是jQuery:Rails应用程序中的jQuery不会读取包含空格的值。如何让它解释这些值?,jquery,ruby-on-rails,Jquery,Ruby On Rails,我正在尝试使用jQuery自动更新Rails应用程序中的相关选择框。它适用于没有空格的值,如“Barahona”或“Manzanillo”,但当我尝试“Puerto Plata”或“Santo Domingo”时,它失败了 这里是当我选择“Puerto Plata”时控制台出现的错误 我使用的jQuery是Railscasts#88中采用的,并在此处列出,用于选择一个港口,然后过滤泊位,以仅列出该港口的泊位 以下是jQuery: jQuery -> berths = $('#voyag
jQuery ->
berths = $('#voyage_berth_id').html()
console.log(berths)
$('#voyage_port_id').change ->
port = $('#voyage_port_id :selected').text()
options = $(berths).filter("optgroup[label=#{port}]").html()
console.log(options)
if options
$('#voyage_berth_id').html(options)
$('#voyage_berth_id').parent().show()
else
$('#voyage_berth_id').empty()
$('#voyage_berth_id').parent().hide()
以下是视图:
<%= f.label :port_id %>
<%= f.collection_select :port_id, Port.order(:name), :id, :name %>
<%= f.label :berth_id %>
<%# f.collection_select :berth_id, Berth.order(:name), :id, :name %>
<%= f.grouped_collection_select :berth_id, Port.order(:name), :berths, :name, :id, :name %>
您可能希望在带有空格的标签周围使用单引号。所以你的表情会变成这样
options = $(berths).filter("optgroup[label='#{port}']").html()
您可能希望在带有空格的标签周围使用单引号。所以你的表情会变成这样
options = $(berths).filter("optgroup[label='#{port}']").html()