form.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /**
  2. @Name:layui.form 表单组件
  3. @Author:贤心
  4. @License:MIT
  5. */
  6. layui.define('layer', function(exports){
  7. "use strict";
  8. var $ = layui.$
  9. ,layer = layui.layer
  10. ,hint = layui.hint()
  11. ,device = layui.device()
  12. ,MOD_NAME = 'form', ELEM = '.layui-form', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'layui-disabled'
  13. ,Form = function(){
  14. this.config = {
  15. verify: {
  16. required: [
  17. /[\S]+/
  18. ,'必填项不能为空'
  19. ]
  20. ,phone: [
  21. /^1\d{10}$/
  22. ,'请输入正确的手机号'
  23. ]
  24. ,email: [
  25. /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
  26. ,'邮箱格式不正确'
  27. ]
  28. ,url: [
  29. /(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/
  30. ,'链接格式不正确'
  31. ]
  32. ,number: function(value){
  33. if(!value || isNaN(value)) return '只能填写数字'
  34. }
  35. ,date: [
  36. /^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/
  37. ,'日期格式不正确'
  38. ]
  39. ,identity: [
  40. /(^\d{15}$)|(^\d{17}(x|X|\d)$)/
  41. ,'请输入正确的身份证号'
  42. ]
  43. }
  44. };
  45. };
  46. //全局设置
  47. Form.prototype.set = function(options){
  48. var that = this;
  49. $.extend(true, that.config, options);
  50. return that;
  51. };
  52. //验证规则设定
  53. Form.prototype.verify = function(settings){
  54. var that = this;
  55. $.extend(true, that.config.verify, settings);
  56. return that;
  57. };
  58. //表单事件监听
  59. Form.prototype.on = function(events, callback){
  60. return layui.onevent.call(this, MOD_NAME, events, callback);
  61. };
  62. //表单控件渲染
  63. Form.prototype.render = function(type, filter){
  64. var that = this
  65. ,elemForm = $(ELEM + function(){
  66. return filter ? ('[lay-filter="' + filter +'"]') : '';
  67. }())
  68. ,items = {
  69. //下拉选择框
  70. select: function(){
  71. var TIPS = '请选择', CLASS = 'layui-form-select', TITLE = 'layui-select-title'
  72. ,NONE = 'layui-select-none', initValue = '', thatInput
  73. ,selects = elemForm.find('select'), hide = function(e, clear){
  74. if(!$(e.target).parent().hasClass(TITLE) || clear){
  75. $('.'+CLASS).removeClass(CLASS+'ed ' + CLASS+'up');
  76. thatInput && initValue && thatInput.val(initValue);
  77. }
  78. thatInput = null;
  79. }
  80. ,events = function(reElem, disabled, isSearch){
  81. var select = $(this)
  82. ,title = reElem.find('.' + TITLE)
  83. ,input = title.find('input')
  84. ,dl = reElem.find('dl')
  85. ,dds = dl.children('dd')
  86. if(disabled) return;
  87. //展开下拉
  88. var showDown = function(){
  89. var top = reElem.offset().top + reElem.outerHeight() + 5 - win.scrollTop()
  90. ,dlHeight = dl.outerHeight();
  91. reElem.addClass(CLASS+'ed');
  92. dds.removeClass(HIDE);
  93. //上下定位识别
  94. if(top + dlHeight > win.height() && top >= dlHeight){
  95. reElem.addClass(CLASS + 'up');
  96. }
  97. }, hideDown = function(choose){
  98. reElem.removeClass(CLASS+'ed ' + CLASS+'up');
  99. input.blur();
  100. if(choose) return;
  101. notOption(input.val(), function(none){
  102. if(none){
  103. initValue = dl.find('.'+THIS).html();
  104. input && input.val(initValue);
  105. }
  106. });
  107. };
  108. //点击标题区域
  109. title.on('click', function(e){
  110. reElem.hasClass(CLASS+'ed') ? (
  111. hideDown()
  112. ) : (
  113. hide(e, true),
  114. showDown()
  115. );
  116. dl.find('.'+NONE).remove();
  117. });
  118. //点击箭头获取焦点
  119. title.find('.layui-edge').on('click', function(){
  120. input.focus();
  121. });
  122. //键盘事件
  123. input.on('keyup', function(e){
  124. var keyCode = e.keyCode;
  125. //Tab键
  126. if(keyCode === 9){
  127. showDown();
  128. }
  129. }).on('keydown', function(e){
  130. var keyCode = e.keyCode;
  131. //Tab键
  132. if(keyCode === 9){
  133. hideDown();
  134. } else if(keyCode === 13){ //回车键
  135. e.preventDefault();
  136. }
  137. });
  138. //检测值是否不属于select项
  139. var notOption = function(value, callback, origin){
  140. var num = 0;
  141. layui.each(dds, function(){
  142. var othis = $(this)
  143. ,text = othis.text()
  144. ,not = text.indexOf(value) === -1;
  145. if(value === '' || (origin === 'blur') ? value !== text : not) num++;
  146. origin === 'keyup' && othis[not ? 'addClass' : 'removeClass'](HIDE);
  147. });
  148. var none = num === dds.length;
  149. return callback(none), none;
  150. };
  151. //搜索匹配
  152. var search = function(e){
  153. var value = this.value, keyCode = e.keyCode;
  154. if(keyCode === 9 || keyCode === 13
  155. || keyCode === 37 || keyCode === 38
  156. || keyCode === 39 || keyCode === 40
  157. ){
  158. return false;
  159. }
  160. notOption(value, function(none){
  161. if(none){
  162. dl.find('.'+NONE)[0] || dl.append('<p class="'+ NONE +'">无匹配项</p>');
  163. } else {
  164. dl.find('.'+NONE).remove();
  165. }
  166. }, 'keyup');
  167. if(value === ''){
  168. dl.find('.'+NONE).remove();
  169. }
  170. };
  171. if(isSearch){
  172. input.on('keyup', search).on('blur', function(e){
  173. thatInput = input;
  174. initValue = dl.find('.' + THIS).html();
  175. setTimeout(function(){
  176. notOption(input.val(), function(none){
  177. initValue || input.val(''); //none && !initValue
  178. }, 'blur');
  179. }, 200);
  180. });
  181. }
  182. //选择
  183. dds.on('click', function(){
  184. var othis = $(this), value = othis.attr('lay-value');
  185. var filter = select.attr('lay-filter'); //获取过滤器
  186. if(othis.hasClass(DISABLED)) return false;
  187. if(othis.hasClass('layui-select-tips')){
  188. input.val('');
  189. } else {
  190. input.val(othis.text());
  191. othis.addClass(THIS);
  192. }
  193. othis.siblings().removeClass(THIS);
  194. select.val(value).removeClass('layui-form-danger')
  195. layui.event.call(this, MOD_NAME, 'select('+ filter +')', {
  196. elem: select[0]
  197. ,value: value
  198. ,othis: reElem
  199. });
  200. hideDown(true);
  201. return false;
  202. });
  203. reElem.find('dl>dt').on('click', function(e){
  204. return false;
  205. });
  206. //关闭下拉
  207. $(document).off('click', hide).on('click', hide);
  208. }
  209. selects.each(function(index, select){
  210. var othis = $(this)
  211. ,hasRender = othis.next('.'+CLASS)
  212. ,disabled = this.disabled
  213. ,value = select.value
  214. ,selected = $(select.options[select.selectedIndex]) //获取当前选中项
  215. ,optionsFirst = select.options[0];
  216. if(typeof othis.attr('lay-ignore') === 'string') return othis.show();
  217. var isSearch = typeof othis.attr('lay-search') === 'string'
  218. ,placeholder = optionsFirst ? (
  219. optionsFirst.value ? TIPS : (optionsFirst.innerHTML || TIPS)
  220. ) : TIPS;
  221. //替代元素
  222. var reElem = $(['<div class="'+ (isSearch ? '' : 'layui-unselect ') + CLASS + (disabled ? ' layui-select-disabled' : '') +'">'
  223. ,'<div class="'+ TITLE +'"><input type="text" placeholder="'+ placeholder +'" value="'+ (value ? selected.html() : '') +'" '+ (isSearch ? '' : 'readonly') +' class="layui-input'+ (isSearch ? '' : ' layui-unselect') + (disabled ? (' ' + DISABLED) : '') +'">'
  224. ,'<i class="layui-edge"></i></div>'
  225. ,'<dl class="layui-anim layui-anim-upbit'+ (othis.find('optgroup')[0] ? ' layui-select-group' : '') +'">'+ function(options){
  226. var arr = [];
  227. layui.each(options, function(index, item){
  228. if(index === 0 && !item.value){
  229. arr.push('<dd lay-value="" class="layui-select-tips">'+ (item.innerHTML || TIPS) +'</dd>');
  230. } else if(item.tagName.toLowerCase() === 'optgroup'){
  231. arr.push('<dt>'+ item.label +'</dt>');
  232. } else {
  233. arr.push('<dd lay-value="'+ item.value +'" class="'+ (value === item.value ? THIS : '') + (item.disabled ? (' '+DISABLED) : '') +'">'+ item.innerHTML +'</dd>');
  234. }
  235. });
  236. arr.length === 0 && arr.push('<dd lay-value="" class="'+ DISABLED +'">没有选项</dd>');
  237. return arr.join('');
  238. }(othis.find('*')) +'</dl>'
  239. ,'</div>'].join(''));
  240. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  241. othis.after(reElem);
  242. events.call(this, reElem, disabled, isSearch);
  243. });
  244. }
  245. //复选框/开关
  246. ,checkbox: function(){
  247. var CLASS = {
  248. checkbox: ['layui-form-checkbox', 'layui-form-checked', 'checkbox']
  249. ,_switch: ['layui-form-switch', 'layui-form-onswitch', 'switch']
  250. }
  251. ,checks = elemForm.find('input[type=checkbox]')
  252. ,events = function(reElem, RE_CLASS){
  253. var check = $(this);
  254. //勾选
  255. reElem.on('click', function(){
  256. var filter = check.attr('lay-filter') //获取过滤器
  257. ,text = (check.attr('lay-text')||'').split('|');
  258. if(check[0].disabled) return;
  259. check[0].checked ? (
  260. check[0].checked = false
  261. ,reElem.removeClass(RE_CLASS[1]).find('em').text(text[1])
  262. ) : (
  263. check[0].checked = true
  264. ,reElem.addClass(RE_CLASS[1]).find('em').text(text[0])
  265. );
  266. layui.event.call(check[0], MOD_NAME, RE_CLASS[2]+'('+ filter +')', {
  267. elem: check[0]
  268. ,value: check[0].value
  269. ,othis: reElem
  270. });
  271. });
  272. }
  273. checks.each(function(index, check){
  274. var othis = $(this), skin = othis.attr('lay-skin')
  275. ,text = (othis.attr('lay-text')||'').split('|'), disabled = this.disabled;
  276. if(skin === 'switch') skin = '_'+skin;
  277. var RE_CLASS = CLASS[skin] || CLASS.checkbox;
  278. if(typeof othis.attr('lay-ignore') === 'string') return othis.show();
  279. //替代元素
  280. var hasRender = othis.next('.' + RE_CLASS[0]);
  281. var reElem = $(['<div class="layui-unselect '+ RE_CLASS[0] + (
  282. check.checked ? (' '+RE_CLASS[1]) : '') + (disabled ? ' layui-checkbox-disbaled '+DISABLED : '') +'" lay-skin="'+ (skin||'') +'">'
  283. ,{
  284. _switch: '<em>'+ ((check.checked ? text[0] : text[1])||'') +'</em><i></i>'
  285. }[skin] || ((check.title.replace(/\s/g, '') ? ('<span>'+ check.title +'</span>') : '') +'<i class="layui-icon">'+ (skin ? '&#xe605;' : '&#xe618;') +'</i>')
  286. ,'</div>'].join(''));
  287. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  288. othis.after(reElem);
  289. events.call(this, reElem, RE_CLASS);
  290. });
  291. }
  292. //单选框
  293. ,radio: function(){
  294. var CLASS = 'layui-form-radio', ICON = ['&#xe643;', '&#xe63f;']
  295. ,radios = elemForm.find('input[type=radio]')
  296. ,events = function(reElem){
  297. var radio = $(this), ANIM = 'layui-anim-scaleSpring';
  298. reElem.on('click', function(){
  299. var name = radio[0].name, forms = radio.parents(ELEM);
  300. var filter = radio.attr('lay-filter'); //获取过滤器
  301. var sameRadio = forms.find('input[name='+ name.replace(/(\.|#|\[|\])/g, '\\$1') +']'); //找到相同name的兄弟
  302. if(radio[0].disabled) return;
  303. layui.each(sameRadio, function(){
  304. var next = $(this).next('.'+CLASS);
  305. this.checked = false;
  306. next.removeClass(CLASS+'ed');
  307. next.find('.layui-icon').removeClass(ANIM).html(ICON[1]);
  308. });
  309. radio[0].checked = true;
  310. reElem.addClass(CLASS+'ed');
  311. reElem.find('.layui-icon').addClass(ANIM).html(ICON[0]);
  312. layui.event.call(radio[0], MOD_NAME, 'radio('+ filter +')', {
  313. elem: radio[0]
  314. ,value: radio[0].value
  315. ,othis: reElem
  316. });
  317. });
  318. };
  319. radios.each(function(index, radio){
  320. var othis = $(this), hasRender = othis.next('.' + CLASS), disabled = this.disabled;
  321. if(typeof othis.attr('lay-ignore') === 'string') return othis.show();
  322. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  323. //替代元素
  324. var reElem = $(['<div class="layui-unselect '+ CLASS + (radio.checked ? (' '+CLASS+'ed') : '') + (disabled ? ' layui-radio-disbaled '+DISABLED : '') +'">'
  325. ,'<i class="layui-anim layui-icon">'+ ICON[radio.checked ? 0 : 1] +'</i>'
  326. ,'<div>'+ function(){
  327. var title = radio.title || '';
  328. if(typeof othis.next().attr('lay-radio') === 'string'){
  329. title = othis.next().html();
  330. othis.next().remove();
  331. }
  332. return title
  333. }() +'</div>'
  334. ,'</div>'].join(''));
  335. othis.after(reElem);
  336. events.call(this, reElem);
  337. });
  338. }
  339. };
  340. type ? (
  341. items[type] ? items[type]() : hint.error('不支持的'+ type + '表单渲染')
  342. ) : layui.each(items, function(index, item){
  343. item();
  344. });
  345. return that;
  346. };
  347. //表单提交校验
  348. var submit = function(){
  349. var button = $(this), verify = form.config.verify, stop = null
  350. ,DANGER = 'layui-form-danger', field = {} ,elem = button.parents(ELEM)
  351. ,verifyElem = elem.find('*[lay-verify]') //获取需要校验的元素
  352. ,formElem = button.parents('form')[0] //获取当前所在的form元素,如果存在的话
  353. ,fieldElem = elem.find('input,select,textarea') //获取所有表单域
  354. ,filter = button.attr('lay-filter'); //获取过滤器
  355. //开始校验
  356. layui.each(verifyElem, function(_, item){
  357. var othis = $(this)
  358. ,vers = othis.attr('lay-verify').split('|')
  359. ,verType = othis.attr('lay-verType') //提示方式
  360. ,value = othis.val();
  361. othis.removeClass(DANGER);
  362. layui.each(vers, function(_, thisVer){
  363. var isTrue //是否命中校验
  364. ,errorText = '' //错误提示文本
  365. ,isFn = typeof verify[thisVer] === 'function';
  366. //匹配验证规则
  367. if(verify[thisVer]){
  368. var isTrue = isFn ? errorText = verify[thisVer](value, item) : !verify[thisVer][0].test(value);
  369. errorText = errorText || verify[thisVer][1];
  370. //如果是必填项或者非空命中校验,则阻止提交,弹出提示
  371. if(isTrue){
  372. //提示层风格
  373. if(verType === 'tips'){
  374. layer.tips(errorText, function(){
  375. if(typeof othis.attr('lay-ignore') !== 'string'){
  376. if(item.tagName.toLowerCase() === 'select' || /^checkbox|radio$/.test(item.type)){
  377. return othis.next();
  378. }
  379. }
  380. return othis;
  381. }(), {tips: 1});
  382. } else if(verType === 'alert') {
  383. layer.alert(errorText, {title: '提示', shadeClose: true});
  384. } else {
  385. layer.msg(errorText, {icon: 5, shift: 6});
  386. }
  387. if(!device.android && !device.ios) item.focus(); //非移动设备自动定位焦点
  388. othis.addClass(DANGER);
  389. return stop = true;
  390. }
  391. }
  392. });
  393. if(stop) return stop;
  394. });
  395. if(stop) return false;
  396. var nameIndex = {}; //数组 name 索引
  397. layui.each(fieldElem, function(_, item){
  398. item.name = (item.name || '').replace(/^\s*|\s*&/, '');
  399. if(!item.name) return;
  400. //用于支持数组 name
  401. if(/^.*\[\]$/.test(item.name)){
  402. var key = item.name.match(/^(.*)\[\]$/g)[0];
  403. nameIndex[key] = nameIndex[key] | 0;
  404. item.name = item.name.replace(/^(.*)\[\]$/, '$1['+ (nameIndex[key]++) +']');
  405. }
  406. if(/^checkbox|radio$/.test(item.type) && !item.checked) return;
  407. field[item.name] = item.value;
  408. });
  409. //获取字段
  410. return layui.event.call(this, MOD_NAME, 'submit('+ filter +')', {
  411. elem: this
  412. ,form: formElem
  413. ,field: field
  414. });
  415. };
  416. //自动完成渲染
  417. var form = new Form()
  418. ,dom = $(document), win = $(window);
  419. form.render();
  420. //表单reset重置渲染
  421. dom.on('reset', ELEM, function(){
  422. var filter = $(this).attr('lay-filter');
  423. setTimeout(function(){
  424. form.render(null, filter);
  425. }, 50);
  426. });
  427. //表单提交事件
  428. dom.on('submit', ELEM, submit)
  429. .on('click', '*[lay-submit]', submit);
  430. exports(MOD_NAME, form);
  431. });