/** @ Name:layui.regionCheckBox 中国省市复选框 @ Author:wanmianji */ ;!function () { 'use strict'; var new_element = document.createElement('script'); new_element.setAttribute('type', 'text/javascript'); new_element.setAttribute('src', layui.cache.base + 'regionCheckBox/data/region.json'); document.body.appendChild(new_element); }(); layui.define('form', function (exports) { 'use strict'; var $ = layui.$ , form = layui.form , MOD_NAME = 'regionCheckBox', ELEM = '.layui-regionContent' , regionCheckBox = { index: layui.regionCheckBox ? (layui.regionCheckBox.index + 10000) : 0 , set: function (options) { var that = this; that.config = $.extend({}, that.config, options); return that; } , on: function (events, callback) { return layui.onevent.call(this, MOD_NAME, events, callback); } } , thisIns = function () { var that = this , options = that.config , id = options.id || options.index; return { reload: function (options) { that.config = $.extend({}, that.config, options); that.render(); } , val: function (valueArr) { setValue(options, valueArr); } , config: options }; } , Class = function (options) { var that = this; that.index = ++regionCheckBox.index; that.config = $.extend({}, that.config, regionCheckBox.config, options); that.render(); }; Class.prototype.config = { value: [] , width: '550px' , border: true , change: function (result) { } , ready: function () { } }; Class.prototype.render = function () { var that = this , options = that.config; options.elem = $(options.elem); var id = options.elem.attr('id'); if (!options.elem.hasClass('layui-form') && options.elem.parents('.layui-form').length == 0) { options.elem.addClass('layui-form'); } options.elem.addClass('layui-regionContent'); options.elem.css('width', options.width); if (!options.border) { options.elem.css('border', 'none'); } options.elem.attr('lay-filter', 'region-' + id); options.elem.html(getCheckBoxs(options)); //初始值 setValue(options, options.value); options.elem.find('.parent').mouseover(function () { $(this).find('.city').show(); }); options.elem.find('.parent').mouseout(function () { $(this).find('.city').hide(); }); form.on('checkbox(regionCheckBox-' + id + ')', function (data) { if (data.elem.value == '所有地域') { //选择所有地域 if (data.elem.checked) { options.elem.find(':checkbox').prop('checked', true); } else { options.elem.find(':checkbox').prop('checked', false); } } else { //选择省(不包括直辖市) if ($(data.elem).parent().hasClass('parent')) { if (data.elem.checked) { $(data.elem).parent().find('.city :checkbox').prop('checked', true); } else { $(data.elem).parent().find('.city :checkbox').prop('checked', false); } } //选择城市 if ($(data.elem).parent().hasClass('city')) { $(data.elem).parents('.parent').attr('name', options.name); if (data.elem.checked) { var is_all = true; $(data.elem).parent().find(':checkbox').each(function (i, item) { if (!item.checked) { is_all = false; return false; } }); if (is_all) { $(data.elem).parents('.parent').find(':checkbox:first').prop('checked', true); } } else { $(data.elem).parents('.parent').find(':checkbox:first').prop('checked', false); } } //选择除所有地域外任意 if (data.elem.checked) { var is_all = true; options.elem.find(':checkbox[value!="所有地域"]').each(function (i, item) { if (!item.checked) { is_all = false; return false; } }); if (is_all) { options.elem.find(':checkbox[value="所有地域"]').prop('checked', true); } } else { options.elem.find(':checkbox[value="所有地域"]').prop('checked', false); } } form.render('checkbox', options.elem.attr('lay-filter')); renderParentDom(options.elem); initName(options); options.change(data); }); options.ready(); } function getCheckBoxs(options) { var name = options.name, id = options.elem.attr('id'), skin = 'primary'; var boxs = '