Created
September 2, 2011 14:08
-
-
Save liwh/1188679 to your computer and use it in GitHub Desktop.
district
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 # 级联回显 |
这个方法有两个作用:
- 普通用法,级联选择
- 当所有下拉框的值已经有了,需要回显时,级联选中它们(会用到传入的seed种子值)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
selects.change -> 之后的代码逻辑:
$this为当前下拉框对象
select下一个下拉框对象(下级)
如果当前下拉框有值 并且 右边紧接着一个下拉框(下级),则获取下级的选项
成功获取下级选项后,如果 seed中有对象的值,则选中此值,并级联触发...