Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rails应用程序中的jQuery不会读取包含空格的值。如何让它解释这些值?_Jquery_Ruby On Rails - Fatal编程技术网

Rails应用程序中的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自动更新Rails应用程序中的相关选择框。它适用于没有空格的值,如“Barahona”或“Manzanillo”,但当我尝试“Puerto Plata”或“Santo Domingo”时,它失败了

这里是当我选择“Puerto Plata”时控制台出现的错误

我使用的jQuery是Railscasts#88中采用的,并在此处列出,用于选择一个港口,然后过滤泊位,以仅列出该港口的泊位

以下是jQuery:

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()