jQuery实现的多选框多级联动插件

2022-10-22,,,

jquery 实现的多选联动插件

. 代码如下:

// 使用:$(_event_src_).autoselect(_reload_, reload_url);
// 前台用get方法传输<select>标签的属性"name"和选中<option>的属性"value"
// 后台用json格式传输数据
// 格式: { value:<option>的属性"value", text:<option>的显示文本 }
(function($) {
$.fn.extend({
autoselect: function(dest, url) {
return this.each(function() {
$.selectchange($(this), $(dest), url);
});
},
});

// 重置复选框
$.selectreset = function(target) {
if (target != null) {
$.selectreset(target.data("nextselect"));
target.empty();
target.append(target.data("defaultopt"));
}
};

// 加载复选框
$.selectload = function(target, data) {
$.each(data, function(index, content) {
var option = $("<option></option>")
.attr("value", content.value).text(content.text);
target.append(option);
});
};

// 绑定 change 事件
$.selectchange = function(target, dest, url) {
// 绑定联动链
target.data("nextselect", dest);

// 记录默认选项(first option)
if (target.data("defaultopt") == null)
target.data("defaultopt", target.children().first());
dest.data("defaultopt", dest.children().first());

$(document).ready(function() {
target.change(function(event) {
var _target = event.target || window.event.srcelement;
if (_target.value != target.data("defaultopt").attr("value")) {
$.getjson(url, {
"name": _target.name,
"value": _target.value
}, function(data, status) {
if (status == "success") {
$.selectreset(target.data("nextselect"));
$.selectload(target.data("nextselect"), data);
}
}); // 后台以 json 格式传输数据
} else {
$.selectreset(target.data("nextselect"));
}
});
});
};
})(jquery);

《jQuery实现的多选框多级联动插件.doc》

下载本文的Word格式文档,以方便收藏与打印。