form.js 25 KB

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