Skip to content

Instantly share code, notes, and snippets.

@liwh
Created September 2, 2011 14:08
Show Gist options
  • Save liwh/1188679 to your computer and use it in GitHub Desktop.
Save liwh/1188679 to your computer and use it in GitHub Desktop.
district
@RegionUtils =
init: (seed = [], region = '.region') ->
$(region).each ->
selects = $('select', this)
selects.unbind('change') # 避免多次绑定change事件
selects.change ->
$this = this
select_index = selects.index($this) + 1
select = selects.eq(select_index)
if $(this).val() and select[0]
$.get "/district/" + $(this).val(), (data) ->
result = eval(data)
options = select[0].options
$("option:gt(0)", select).remove()
$.each result, (i, item) -> options.add new Option(item[0], item[1])
value = seed[select_index]
select.val(value).change() if value # 级联回显
@saberma
Copy link

saberma commented Sep 2, 2011

selects.change -> 之后的代码逻辑:
$this为当前下拉框对象
select下一个下拉框对象(下级)
如果当前下拉框有值 并且 右边紧接着一个下拉框(下级),则获取下级的选项
成功获取下级选项后,如果 seed中有对象的值,则选中此值,并级联触发...

@saberma
Copy link

saberma commented Sep 2, 2011

这个方法有两个作用:

  1. 普通用法,级联选择
  2. 当所有下拉框的值已经有了,需要回显时,级联选中它们(会用到传入的seed种子值)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment