form.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*!
  2. * form 表单组件
  3. * MIT Licensed
  4. */
  5. layui.define('layer', function(exports){
  6. "use strict";
  7. var $ = layui.$
  8. ,layer = layui.layer
  9. ,hint = layui.hint()
  10. ,device = layui.device()
  11. ,MOD_NAME = 'form', ELEM = '.layui-form', THIS = 'layui-this'
  12. ,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. ,autocomplete: null //全局 autocomplete 状态。null 表示不干预
  45. };
  46. };
  47. //全局设置
  48. Form.prototype.set = function(options){
  49. var that = this;
  50. $.extend(true, that.config, options);
  51. return that;
  52. };
  53. //验证规则设定
  54. Form.prototype.verify = function(settings){
  55. var that = this;
  56. $.extend(true, that.config.verify, settings);
  57. return that;
  58. };
  59. //表单事件
  60. Form.prototype.on = function(events, callback){
  61. return layui.onevent.call(this, MOD_NAME, events, callback);
  62. };
  63. //赋值/取值
  64. Form.prototype.val = function(filter, object){
  65. var that = this
  66. ,formElem = $(ELEM + '[lay-filter="' + filter +'"]');
  67. //遍历
  68. formElem.each(function(index, item){
  69. var itemForm = $(this);
  70. //赋值
  71. layui.each(object, function(key, value){
  72. var itemElem = itemForm.find('[name="'+ key +'"]')
  73. ,type;
  74. //如果对应的表单不存在,则不执行
  75. if(!itemElem[0]) return;
  76. type = itemElem[0].type;
  77. //如果为复选框
  78. if(type === 'checkbox'){
  79. itemElem[0].checked = value;
  80. } else if(type === 'radio') { //如果为单选框
  81. itemElem.each(function(){
  82. if(this.value == value ){
  83. this.checked = true
  84. }
  85. });
  86. } else { //其它类型的表单
  87. itemElem.val(value);
  88. }
  89. });
  90. });
  91. form.render(null, filter);
  92. //返回值
  93. return that.getValue(filter);
  94. };
  95. //取值
  96. Form.prototype.getValue = function(filter, itemForm){
  97. itemForm = itemForm || $(ELEM + '[lay-filter="' + filter +'"]').eq(0);
  98. var nameIndex = {} //数组 name 索引
  99. ,field = {}
  100. ,fieldElem = itemForm.find('input,select,textarea') //获取所有表单域
  101. layui.each(fieldElem, function(_, item){
  102. var othis = $(this)
  103. ,init_name; //初始 name
  104. item.name = (item.name || '').replace(/^\s*|\s*&/, '');
  105. if(!item.name) return;
  106. //用于支持数组 name
  107. if(/^.*\[\]$/.test(item.name)){
  108. var key = item.name.match(/^(.*)\[\]$/g)[0];
  109. nameIndex[key] = nameIndex[key] | 0;
  110. init_name = item.name.replace(/^(.*)\[\]$/, '$1['+ (nameIndex[key]++) +']');
  111. }
  112. if(/^checkbox|radio$/.test(item.type) && !item.checked) return; //复选框和单选框未选中,不记录字段
  113. field[init_name || item.name] = item.value;
  114. });
  115. return field;
  116. };
  117. //表单控件渲染
  118. Form.prototype.render = function(type, filter){
  119. var that = this
  120. ,options = that.config
  121. ,elemForm = $(ELEM + function(){
  122. return filter ? ('[lay-filter="' + filter +'"]') : '';
  123. }())
  124. ,items = {
  125. //输入框
  126. input: function(){
  127. var inputs = elemForm.find('input,textarea');
  128. //初始化全局的 autocomplete
  129. options.autocomplete && inputs.attr('autocomplete', options.autocomplete);
  130. }
  131. //下拉选择框
  132. ,select: function(){
  133. var TIPS = '请选择', CLASS = 'layui-form-select', TITLE = 'layui-select-title'
  134. ,NONE = 'layui-select-none', initValue = '', thatInput
  135. ,selects = elemForm.find('select')
  136. //隐藏 select
  137. ,hide = function(e, clear){
  138. if(!$(e.target).parent().hasClass(TITLE) || clear){
  139. $('.'+CLASS).removeClass(CLASS+'ed ' + CLASS+'up');
  140. thatInput && initValue && thatInput.val(initValue);
  141. }
  142. thatInput = null;
  143. }
  144. //各种事件
  145. ,events = function(reElem, disabled, isSearch){
  146. var select = $(this)
  147. ,title = reElem.find('.' + TITLE)
  148. ,input = title.find('input')
  149. ,dl = reElem.find('dl')
  150. ,dds = dl.children('dd')
  151. ,index = this.selectedIndex //当前选中的索引
  152. ,nearElem; //select 组件当前选中的附近元素,用于辅助快捷键功能
  153. if(disabled) return;
  154. //展开下拉
  155. var showDown = function(){
  156. var top = reElem.offset().top + reElem.outerHeight() + 5 - $win.scrollTop()
  157. ,dlHeight = dl.outerHeight();
  158. index = select[0].selectedIndex; //获取最新的 selectedIndex
  159. reElem.addClass(CLASS+'ed');
  160. dds.removeClass(HIDE);
  161. nearElem = null;
  162. //初始选中样式
  163. dds.eq(index).addClass(THIS).siblings().removeClass(THIS);
  164. //上下定位识别
  165. if(top + dlHeight > $win.height() && top >= dlHeight){
  166. reElem.addClass(CLASS + 'up');
  167. }
  168. followScroll();
  169. }
  170. //隐藏下拉
  171. ,hideDown = function(choose){
  172. reElem.removeClass(CLASS+'ed ' + CLASS+'up');
  173. input.blur();
  174. nearElem = null;
  175. if(choose) return;
  176. notOption(input.val(), function(none){
  177. var selectedIndex = select[0].selectedIndex;
  178. //未查询到相关值
  179. if(none){
  180. initValue = $(select[0].options[selectedIndex]).html(); //重新获得初始选中值
  181. //如果是第一项,且文本值等于 placeholder,则清空初始值
  182. if(selectedIndex === 0 && initValue === input.attr('placeholder')){
  183. initValue = '';
  184. };
  185. //如果有选中值,则将输入框纠正为该值。否则清空输入框
  186. input.val(initValue || '');
  187. }
  188. });
  189. }
  190. //定位下拉滚动条
  191. ,followScroll = function(){
  192. var thisDd = dl.children('dd.'+ THIS);
  193. if(!thisDd[0]) return;
  194. var posTop = thisDd.position().top
  195. ,dlHeight = dl.height()
  196. ,ddHeight = thisDd.height();
  197. //若选中元素在滚动条不可见底部
  198. if(posTop > dlHeight){
  199. dl.scrollTop(posTop + dl.scrollTop() - dlHeight + ddHeight - 5);
  200. }
  201. //若选择玄素在滚动条不可见顶部
  202. if(posTop < 0){
  203. dl.scrollTop(posTop + dl.scrollTop() - 5);
  204. }
  205. };
  206. //点击标题区域
  207. title.on('click', function(e){
  208. reElem.hasClass(CLASS+'ed') ? (
  209. hideDown()
  210. ) : (
  211. hide(e, true),
  212. showDown()
  213. );
  214. dl.find('.'+NONE).remove();
  215. });
  216. //点击箭头获取焦点
  217. title.find('.layui-edge').on('click', function(){
  218. input.focus();
  219. });
  220. //select 中 input 键盘事件
  221. input.on('keyup', function(e){ //键盘松开
  222. var keyCode = e.keyCode;
  223. //Tab键展开
  224. if(keyCode === 9){
  225. showDown();
  226. }
  227. }).on('keydown', function(e){ //键盘按下
  228. var keyCode = e.keyCode;
  229. //Tab键隐藏
  230. if(keyCode === 9){
  231. hideDown();
  232. }
  233. //标注 dd 的选中状态
  234. var setThisDd = function(prevNext, thisElem1){
  235. var nearDd, cacheNearElem
  236. e.preventDefault();
  237. //得到当前队列元素
  238. var thisElem = function(){
  239. var thisDd = dl.children('dd.'+ THIS);
  240. //如果是搜索状态,且按 Down 键,且当前可视 dd 元素在选中元素之前,
  241. //则将当前可视 dd 元素的上一个元素作为虚拟的当前选中元素,以保证递归不中断
  242. if(dl.children('dd.'+ HIDE)[0] && prevNext === 'next'){
  243. var showDd = dl.children('dd:not(.'+ HIDE +',.'+ DISABLED +')')
  244. ,firstIndex = showDd.eq(0).index();
  245. if(firstIndex >=0 && firstIndex < thisDd.index() && !showDd.hasClass(THIS)){
  246. return showDd.eq(0).prev()[0] ? showDd.eq(0).prev() : dl.children(':last');
  247. }
  248. }
  249. if(thisElem1 && thisElem1[0]){
  250. return thisElem1;
  251. }
  252. if(nearElem && nearElem[0]){
  253. return nearElem;
  254. }
  255. return thisDd;
  256. //return dds.eq(index);
  257. }();
  258. cacheNearElem = thisElem[prevNext](); //当前元素的附近元素
  259. nearDd = thisElem[prevNext]('dd:not(.'+ HIDE +')'); //当前可视元素的 dd 元素
  260. //如果附近的元素不存在,则停止执行,并清空 nearElem
  261. if(!cacheNearElem[0]) return nearElem = null;
  262. //记录附近的元素,让其成为下一个当前元素
  263. nearElem = thisElem[prevNext]();
  264. //如果附近不是 dd ,或者附近的 dd 元素是禁用状态,则进入递归查找
  265. if((!nearDd[0] || nearDd.hasClass(DISABLED)) && nearElem[0]){
  266. return setThisDd(prevNext, nearElem);
  267. }
  268. nearDd.addClass(THIS).siblings().removeClass(THIS); //标注样式
  269. followScroll(); //定位滚动条
  270. };
  271. if(keyCode === 38) setThisDd('prev'); //Up 键
  272. if(keyCode === 40) setThisDd('next'); //Down 键
  273. //Enter 键
  274. if(keyCode === 13){
  275. e.preventDefault();
  276. dl.children('dd.'+THIS).trigger('click');
  277. }
  278. });
  279. //检测值是否不属于 select 项
  280. var notOption = function(value, callback, origin){
  281. var num = 0;
  282. layui.each(dds, function(){
  283. var othis = $(this)
  284. ,text = othis.text()
  285. ,not = text.indexOf(value) === -1;
  286. if(value === '' || (origin === 'blur') ? value !== text : not) num++;
  287. origin === 'keyup' && othis[not ? 'addClass' : 'removeClass'](HIDE);
  288. });
  289. var none = num === dds.length;
  290. return callback(none), none;
  291. };
  292. //搜索匹配
  293. var search = function(e){
  294. var value = this.value, keyCode = e.keyCode;
  295. if(keyCode === 9 || keyCode === 13
  296. || keyCode === 37 || keyCode === 38
  297. || keyCode === 39 || keyCode === 40
  298. ){
  299. return false;
  300. }
  301. notOption(value, function(none){
  302. if(none){
  303. dl.find('.'+NONE)[0] || dl.append('<p class="'+ NONE +'">无匹配项</p>');
  304. } else {
  305. dl.find('.'+NONE).remove();
  306. }
  307. }, 'keyup');
  308. if(value === ''){
  309. dl.find('.'+NONE).remove();
  310. }
  311. followScroll(); //定位滚动条
  312. };
  313. if(isSearch){
  314. input.on('keyup', search).on('blur', function(e){
  315. var selectedIndex = select[0].selectedIndex;
  316. thatInput = input; //当前的 select 中的 input 元素
  317. initValue = $(select[0].options[selectedIndex]).html(); //重新获得初始选中值
  318. //如果是第一项,且文本值等于 placeholder,则清空初始值
  319. if(selectedIndex === 0 && initValue === input.attr('placeholder')){
  320. initValue = '';
  321. };
  322. setTimeout(function(){
  323. notOption(input.val(), function(none){
  324. initValue || input.val(''); //none && !initValue
  325. }, 'blur');
  326. }, 200);
  327. });
  328. }
  329. //选择
  330. dds.on('click', function(){
  331. var othis = $(this), value = othis.attr('lay-value');
  332. var filter = select.attr('lay-filter'); //获取过滤器
  333. if(othis.hasClass(DISABLED)) return false;
  334. if(othis.hasClass('layui-select-tips')){
  335. input.val('');
  336. } else {
  337. input.val(othis.text());
  338. othis.addClass(THIS);
  339. }
  340. othis.siblings().removeClass(THIS);
  341. select.val(value).removeClass('layui-form-danger')
  342. layui.event.call(this, MOD_NAME, 'select('+ filter +')', {
  343. elem: select[0]
  344. ,value: value
  345. ,othis: reElem
  346. });
  347. hideDown(true);
  348. return false;
  349. });
  350. reElem.find('dl>dt').on('click', function(e){
  351. return false;
  352. });
  353. $(document).off('click', hide).on('click', hide); //点击其它元素关闭 select
  354. }
  355. selects.each(function(index, select){
  356. var othis = $(this)
  357. ,hasRender = othis.next('.'+CLASS)
  358. ,disabled = this.disabled
  359. ,value = select.value
  360. ,selected = $(select.options[select.selectedIndex]) //获取当前选中项
  361. ,optionsFirst = select.options[0];
  362. if(typeof othis.attr('lay-ignore') === 'string') return othis.show();
  363. var isSearch = typeof othis.attr('lay-search') === 'string'
  364. ,placeholder = optionsFirst ? (
  365. optionsFirst.value ? TIPS : (optionsFirst.innerHTML || TIPS)
  366. ) : TIPS;
  367. //替代元素
  368. var reElem = $(['<div class="'+ (isSearch ? '' : 'layui-unselect ') + CLASS
  369. ,(disabled ? ' layui-select-disabled' : '') +'">'
  370. ,'<div class="'+ TITLE +'">'
  371. ,('<input type="text" placeholder="'+ $.trim(placeholder) +'" '
  372. +('value="'+ $.trim(value ? selected.html() : '') +'"') //默认值
  373. +((!disabled && isSearch) ? '' : ' readonly') //是否开启搜索
  374. +' class="layui-input'
  375. +(isSearch ? '' : ' layui-unselect')
  376. + (disabled ? (' ' + DISABLED) : '') +'">') //禁用状态
  377. ,'<i class="layui-edge"></i></div>'
  378. ,'<dl class="layui-anim layui-anim-upbit'+ (othis.find('optgroup')[0] ? ' layui-select-group' : '') +'">'
  379. ,function(options){
  380. var arr = [];
  381. layui.each(options, function(index, item){
  382. if(index === 0 && !item.value){
  383. arr.push('<dd lay-value="" class="layui-select-tips">'+ $.trim(item.innerHTML || TIPS) +'</dd>');
  384. } else if(item.tagName.toLowerCase() === 'optgroup'){
  385. arr.push('<dt>'+ item.label +'</dt>');
  386. } else {
  387. arr.push('<dd lay-value="'+ item.value +'" class="'+ (value === item.value ? THIS : '') + (item.disabled ? (' '+DISABLED) : '') +'">'+ $.trim(item.innerHTML) +'</dd>');
  388. }
  389. });
  390. arr.length === 0 && arr.push('<dd lay-value="" class="'+ DISABLED +'">没有选项</dd>');
  391. return arr.join('');
  392. }(othis.find('*')) +'</dl>'
  393. ,'</div>'].join(''));
  394. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  395. othis.after(reElem);
  396. events.call(this, reElem, disabled, isSearch);
  397. });
  398. }
  399. //复选框/开关
  400. ,checkbox: function(){
  401. var CLASS = {
  402. checkbox: ['layui-form-checkbox', 'layui-form-checked', 'checkbox']
  403. ,_switch: ['layui-form-switch', 'layui-form-onswitch', 'switch']
  404. }
  405. ,checks = elemForm.find('input[type=checkbox]')
  406. ,events = function(reElem, RE_CLASS){
  407. var check = $(this);
  408. //勾选
  409. reElem.on('click', function(){
  410. var filter = check.attr('lay-filter') //获取过滤器
  411. ,text = (check.attr('lay-text')||'').split('|');
  412. if(check[0].disabled) return;
  413. check[0].checked ? (
  414. check[0].checked = false
  415. ,reElem.removeClass(RE_CLASS[1]).find('em').text(text[1])
  416. ) : (
  417. check[0].checked = true
  418. ,reElem.addClass(RE_CLASS[1]).find('em').text(text[0])
  419. );
  420. layui.event.call(check[0], MOD_NAME, RE_CLASS[2]+'('+ filter +')', {
  421. elem: check[0]
  422. ,value: check[0].value
  423. ,othis: reElem
  424. });
  425. });
  426. }
  427. checks.each(function(index, check){
  428. var othis = $(this), skin = othis.attr('lay-skin')
  429. ,text = (othis.attr('lay-text') || '').split('|'), disabled = this.disabled;
  430. if(skin === 'switch') skin = '_'+skin;
  431. var RE_CLASS = CLASS[skin] || CLASS.checkbox;
  432. if(typeof othis.attr('lay-ignore') === 'string') return othis.show();
  433. //替代元素
  434. var hasRender = othis.next('.' + RE_CLASS[0])
  435. ,reElem = $(['<div class="layui-unselect '+ RE_CLASS[0]
  436. ,(check.checked ? (' '+ RE_CLASS[1]) : '') //选中状态
  437. ,(disabled ? ' layui-checkbox-disabled '+ DISABLED : '') //禁用状态
  438. ,'"'
  439. ,(skin ? ' lay-skin="'+ skin +'"' : '') //风格
  440. ,'>'
  441. ,function(){ //不同风格的内容
  442. var title = check.title.replace(/\s/g, '')
  443. ,type = {
  444. //复选框
  445. checkbox: [
  446. (title ? ('<span>'+ check.title +'</span>') : '')
  447. ,'<i class="layui-icon layui-icon-ok"></i>'
  448. ].join('')
  449. //开关
  450. ,_switch: '<em>'+ ((check.checked ? text[0] : text[1]) || '') +'</em><i></i>'
  451. };
  452. return type[skin] || type['checkbox'];
  453. }()
  454. ,'</div>'].join(''));
  455. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  456. othis.after(reElem);
  457. events.call(this, reElem, RE_CLASS);
  458. });
  459. }
  460. //单选框
  461. ,radio: function(){
  462. var CLASS = 'layui-form-radio', ICON = ['&#xe643;', '&#xe63f;']
  463. ,radios = elemForm.find('input[type=radio]')
  464. ,events = function(reElem){
  465. var radio = $(this), ANIM = 'layui-anim-scaleSpring';
  466. reElem.on('click', function(){
  467. var name = radio[0].name, forms = radio.parents(ELEM);
  468. var filter = radio.attr('lay-filter'); //获取过滤器
  469. var sameRadio = forms.find('input[name='+ name.replace(/(\.|#|\[|\])/g, '\\$1') +']'); //找到相同name的兄弟
  470. if(radio[0].disabled) return;
  471. layui.each(sameRadio, function(){
  472. var next = $(this).next('.'+CLASS);
  473. this.checked = false;
  474. next.removeClass(CLASS+'ed');
  475. next.find('.layui-icon').removeClass(ANIM).html(ICON[1]);
  476. });
  477. radio[0].checked = true;
  478. reElem.addClass(CLASS+'ed');
  479. reElem.find('.layui-icon').addClass(ANIM).html(ICON[0]);
  480. layui.event.call(radio[0], MOD_NAME, 'radio('+ filter +')', {
  481. elem: radio[0]
  482. ,value: radio[0].value
  483. ,othis: reElem
  484. });
  485. });
  486. };
  487. radios.each(function(index, radio){
  488. var othis = $(this), hasRender = othis.next('.' + CLASS), disabled = this.disabled;
  489. if(typeof othis.attr('lay-ignore') === 'string') return othis.show();
  490. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  491. //替代元素
  492. var reElem = $(['<div class="layui-unselect '+ CLASS
  493. ,(radio.checked ? (' '+CLASS+'ed') : '') //选中状态
  494. ,(disabled ? ' layui-radio-disabled '+DISABLED : '') +'">' //禁用状态
  495. ,'<i class="layui-anim layui-icon">'+ ICON[radio.checked ? 0 : 1] +'</i>'
  496. ,'<div>'+ function(){
  497. var title = radio.title || '';
  498. if(typeof othis.next().attr('lay-radio') === 'string'){
  499. title = othis.next().html();
  500. //othis.next().remove();
  501. }
  502. return title
  503. }() +'</div>'
  504. ,'</div>'].join(''));
  505. othis.after(reElem);
  506. events.call(this, reElem);
  507. });
  508. }
  509. };
  510. type ? (
  511. items[type] ? items[type]() : hint.error('不支持的 "'+ type + '" 表单渲染')
  512. ) : layui.each(items, function(index, item){
  513. item();
  514. });
  515. return that;
  516. };
  517. //表单提交校验
  518. var submit = function(){
  519. var stop = null //验证不通过状态
  520. ,verify = form.config.verify //验证规则
  521. ,DANGER = 'layui-form-danger' //警示样式
  522. ,field = {} //字段集合
  523. ,button = $(this) //当前触发的按钮
  524. ,elem = button.parents(ELEM).eq(0) //当前所在表单域
  525. ,verifyElem = elem.find('*[lay-verify]') //获取需要校验的元素
  526. ,formElem = button.parents('form')[0] //获取当前所在的 form 元素,如果存在的话
  527. ,filter = button.attr('lay-filter'); //获取过滤器
  528. //开始校验
  529. layui.each(verifyElem, function(_, item){
  530. var othis = $(this)
  531. ,vers = othis.attr('lay-verify').split('|')
  532. ,verType = othis.attr('lay-verType') //提示方式
  533. ,value = othis.val();
  534. othis.removeClass(DANGER); //移除警示样式
  535. //遍历元素绑定的验证规则
  536. layui.each(vers, function(_, thisVer){
  537. var isTrue //是否命中校验
  538. ,errorText = '' //错误提示文本
  539. ,isFn = typeof verify[thisVer] === 'function';
  540. //匹配验证规则
  541. if(verify[thisVer]){
  542. var isTrue = isFn ? errorText = verify[thisVer](value, item) : !verify[thisVer][0].test(value)
  543. //是否属于美化替换后的表单元素
  544. ,isForm2Elem = item.tagName.toLowerCase() === 'select' || /^checkbox|radio$/.test(item.type);
  545. errorText = errorText || verify[thisVer][1];
  546. if(thisVer === 'required'){
  547. errorText = othis.attr('lay-reqText') || errorText;
  548. }
  549. //如果是必填项或者非空命中校验,则阻止提交,弹出提示
  550. if(isTrue){
  551. //提示层风格
  552. if(verType === 'tips'){
  553. layer.tips(errorText, function(){
  554. if(typeof othis.attr('lay-ignore') !== 'string'){
  555. if(isForm2Elem){
  556. return othis.next();
  557. }
  558. }
  559. return othis;
  560. }(), {tips: 1});
  561. } else if(verType === 'alert') {
  562. layer.alert(errorText, {title: '提示', shadeClose: true});
  563. }
  564. //如果返回的为字符或数字,则自动弹出默认提示框;否则由 verify 方法中处理提示
  565. else if(/\bstring|number\b/.test(typeof errorText)){
  566. layer.msg(errorText, {icon: 5, shift: 6});
  567. }
  568. //非移动设备自动定位焦点
  569. if(!device.mobile){
  570. setTimeout(function(){
  571. (isForm2Elem ? othis.next().find('input') : item).focus();
  572. }, 7);
  573. } else { //移动设备定位
  574. $dom.scrollTop(function(){
  575. try {
  576. return (isForm2Elem ? othis.next() : othis).offset().top - 15
  577. } catch(e){
  578. return 0;
  579. }
  580. }());
  581. }
  582. othis.addClass(DANGER);
  583. return stop = true;
  584. }
  585. }
  586. });
  587. if(stop) return stop;
  588. });
  589. if(stop) return false;
  590. //获取当前表单值
  591. field = form.getValue(null, elem);
  592. //返回字段
  593. return layui.event.call(this, MOD_NAME, 'submit('+ filter +')', {
  594. elem: this
  595. ,form: formElem
  596. ,field: field
  597. });
  598. };
  599. //自动完成渲染
  600. var form = new Form()
  601. ,$dom = $(document), $win = $(window);
  602. $(function(){
  603. form.render();
  604. });
  605. //表单reset重置渲染
  606. $dom.on('reset', ELEM, function(){
  607. var filter = $(this).attr('lay-filter');
  608. setTimeout(function(){
  609. form.render(null, filter);
  610. }, 50);
  611. });
  612. //表单提交事件
  613. $dom.on('submit', ELEM, submit)
  614. .on('click', '*[lay-submit]', submit);
  615. exports(MOD_NAME, form);
  616. });