首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails jquery级联下拉重新加载

rails jquery级联下拉重新加载
EN

Stack Overflow用户
提问于 2013-08-11 10:03:01
回答 1查看 417关注 0票数 3

我有一个三层级联下拉列表在rails页面,当国家变化,州或省变化,然后城市变化。

我做了这个功能,分别添加了变化事件到国家,省下拉。不过,我仍须提出以下问题:

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

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

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-31 07:53:39

我知道我有点晚了,但我也遇到了同样的问题,在铁道部找到了解决办法。如果您将Ryan Bates的Railscast用于动态选择框,请在此处找到:

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

评论中的一个用户发布了一个更新的咖啡脚本,以使其与编辑表单一起工作:

代码语言:javascript
复制
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()

也许你还可以使用它,或者其他人觉得这很有帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18170792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档