/** @ Name:layui.regionCheckBox 中国省市复选框 @ Author:wanmianji */ layui.define('form', function(exports){ 'use strict'; var $ = layui.$ ,form = layui.form ,MOD_NAME = 'regionCheckBox', ELEM_CLASS = '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 = { data: [] ,all: ['所有地域', '所有地域'] ,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.addClass('layui-form'); } options.elem.addClass(ELEM_CLASS); 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).parents('.all').length > 0) { //选择全部 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('.province :checkbox').each(function(i, item) { if(! item.checked) { is_all = false; return false; } }); if(is_all) { options.elem.find('.all :checkbox').prop('checked', true); } } else { options.elem.find('.all :checkbox').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 data = options.data, all = options.all, name = options.name, id = options.elem.attr('id'), skin = 'primary', filter = 'regionCheckBox-' + id, boxs = '', hasArea = true; if(all != null && all.length == 2){ boxs = '