我有一个三层级联下拉列表在rails页面,当国家变化,州或省变化,然后城市变化。
我做了这个功能,分别添加了变化事件到国家,省下拉。不过,我仍须提出以下问题:
首先,当重新加载第二层和第三层下拉列表时,如何保留它们。
第二,在编辑时,上次如何加载存储的值?
提前谢谢。
发布于 2013-10-31 07:53:39
我知道我有点晚了,但我也遇到了同样的问题,在铁道部找到了解决办法。如果您将Ryan Bates的Railscast用于动态选择框,请在此处找到:
http://railscasts.com/episodes/88-dynamic-select-menus-revised
评论中的一个用户发布了一个更新的咖啡脚本,以使其与编辑表单一起工作:
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()也许你还可以使用它,或者其他人觉得这很有帮助。
https://stackoverflow.com/questions/18170792
复制相似问题