这是index.js文件内容: layui.define(['form','layer','element'],function(exports){ var $ = layui.$; var form = layui.form; var layer = layui.layer; var element = layui.element; var objList = { ajax:function(filter,url){ form.on(filter,function(data){ $.ajax({ url:url, data:data.field, type:'post', dataType:'json', success:function(result){ if(result.status==0){ layer.msg(result.msg); } } }); return false; }); }, hello:function(str){ $('#btn').click(function(){ layer.msg(str); }); } }; exports('index',objList); });
这是index.html的内容: <script> layui.config({ base: '/static/js/' }).use('index', function () { var index = layui.index; index.ajax('submit(formDemo)',"{:url('index')}"); // index.hello('Hello World'); }); </script>
接收数据我是用TP框架测试的,确实模块化后代码通用性高了,记下来不会忘记了!