<form> <div> <label>尾灯名称</label> <div> <input type="text" required lay-verify="required" placeholder="请输入尾灯名称"> </div> </div> <div> <div> <button type="button"> <i></i>上传图标 </button> <br><br> <div> <img> </div> </div> </div> <div> <div> <button>立即提交</button> </div> </div> </form>
JavaScript代码
<script> layui.use(['form', 'layer', 'upload'], function () { var layer = layui.layer; var upload = layui.upload; var $ = layui.jquery; upload.render({ elem: '#upload', url: '{:U("addTL")}', auto: false,//选择文件后不自动上传 bindAction: '#commit', //上传前的回调 before: function () { this.data = { name: $('input[name="name"]').val() } }, //选择文件后的回调 choose: function (obj) { obj.preview(function (index, file, result) { $('#preview').attr('src', result); }) }, //操作成功的回调 done: function (res, index, upload) { var code = res.code === 0 ? 1 : 2; layer.alert(res.msg, {icon: code}, function () { parent.window.location.reload(); }) }, //上传错误回调 error: function (index, upload) { layer.alert('上传失败!' + index); } }); }) </script>
以上这篇layui 实现表单和文件上传一起传到后台的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章: