gpt4 book ai didi

javascript - #88 动态选择菜单(修订版)在 rails 上不起作用

转载 作者:行者123 更新时间:2023-11-28 08:31:13 25 4
gpt4 key购买 nike

我在执行(railscast 剧集上的动态选择菜单)时遇到问题,因为它无法正常工作。

(州)菜单提供所有可用选项(国家/地区+州)。

我已经按照视频一步一步搜索了可用的解决方案,但没有用,但有一个问题我无法解决。

CoffeeScript (people.js.coffee)

jQuery ->

states = $('#person_state_id').html()
$('#person_state_id').parent().hide()
$('#person_country_id').change ->
country = $('#person_country_id :selected').text()
escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
options = $(states).filter("optgroup[label='#{escaped_country}']").html()
console.log(options)
if options
$('#person_state_id').html(options).parent().show()
else
$('#person_state_id').empty().parent().hide()

new.html.erb

新人

<%= form_for @person do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :country_id %><br />
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: true %>
</div>
<div class="field">
<%= f.label :state_id, "State or Province" %><br />
<%= f.grouped_collection_select :state_id, Country.order(:name), :states, :name, :id, :name, include_blank: true %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>

我还找到了一个更新 Gemfile 的解决方案( gem 'jquery-turbolinks')和 application.js 文件(//= require jquery.turbolinks ),但没用。

请帮忙。

最佳答案

我认为您需要删除此行中的单引号:

options = $(states).filter("optgroup[label='#{escaped_country}']").html()

做到这一点

options = $(states).filter("optgroup[label=#{escaped_country}]").html()

关于javascript - #88 动态选择菜单(修订版)在 rails 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21849996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com