jquery - Rails jquery级联下拉菜单重新加载

标签 jquery ruby-on-rails cascadingdropdown

我在 Rails 页面中有一个三层级联下拉菜单,当国家/地区发生变化、州或省发生变化时,然后城市发生变化。

我通过分别向国家、省份下拉列表添加更改事件来完成该功能。但是,我仍然有问题:

首先,当重新加载时,第二层和第三层下拉列表消失了,如何保留它们。

第二,编辑时,如何加载上次存储的值?

提前致谢。

最佳答案

我知道我有点晚了,但我遇到了同样的问题,并在 Railscasts 找到了解决方案。如果您使用 Ryan Bates 的 Railscast 作为动态选择框,可以在此处找到:

http://railscasts.com/episodes/88-dynamic-select-menus-revised

评论中的一位用户发布了更新的 CoffeeScript ,以使其与编辑表单一起使用:

jQuery ->
  loaded_product = $('#product_category_id :selected').text()
  categorys = $('#product_category_id').html()
  $('#product_category_id').parent().hide()
  if loaded_product.length != 0
   brand = $('#product_brand_id :selected').text()
   escaped_brand = brand.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
   options = $(categorys).filter("optgroup[label=#{escaped_brand}]").html()
   $('#product_category_id').html(options)
   $('#product_category_id').parent().show()
  console.log(categorys)
  $('#product_brand_id').change ->
    brand = $('#product_brand_id :selected').text()
    escaped_brand = brand.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(categorys).filter("optgroup[label=#{escaped_brand}]").html()
    console.log(options)
    if options
      $('#product_category_id').html(options)
      $('#product_category_id').parent().show()
    else
      $('#product_category_id').empty()
      $('#product_category_id').parent().hide()

也许您仍然可以使用它,或者其他人发现这很有帮助。

关于jquery - Rails jquery级联下拉菜单重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18170792/

相关文章:

ruby-on-rails - ruby rails : Capturing JSON data from webhook?

sql - 来自 SQLite 的等效 PostgreSQL 查询

grid - 带有级联下拉列表的剑道 UI 网格

c# - 如何在 MVC4 中获取第二个下拉列表的值?

javascript - 如何使用 jquery 更改元素内的文本?

ruby-on-rails - 在输出为 json 之前过滤模型的属性

javascript - jQuery 标签 : doesn't work (doesn't change page)

asp.net-mvc - ASP.NET MVC - 级联下拉菜单

jquery - 当我使用 PhotoSwipe Jquery 单击特定图像时,新页面打开并显示完整图像

javascript - 使用带有 jQ​​uery 和 ajax 的 Datatables 来收集数据会产生一个奇怪的 URL