标签:pat doc foo sync ret else oct bsd link
背景:使用layui上传文件,在上传成功后的回调函数中调用另一个js函数(validateFormIndexExist(filePath)验证上传文件根目录下是否包含index.html文件),validateFormIndexExist(filePath)需要返回布尔值
编码实现:
1 <!DOCTYPE html> 2 <html> 3 <meta charset="utf-8"> 4 <link href="/layui-v2.5.5/layui/css/layui.css" th:href="@{/layui-v2.5.5/layui/css/layui.css}" rel="stylesheet"> 5 <head th:include="include :: header"></head> 6 <body class="gray-bg"> 7 <button type="button" class="layui-btn" id="uploadFile" ><i class="layui-icon"></i>上传文件</button> 8 <div class="layui-upload-list"> 9 <p id="demoText"></p> 10 </div> 11 <div th:include="include::footer"></div> 12 <script src="/layui-v2.5.5/layui/layui.js" th:src="@{/layui-v2.5.5/layui/layui.js}"></script> 13 <script src="/js/appjs/wsbsdt/itemProjectForm/add.js" th:src="@{/js/appjs/wsbsdt/itemProjectForm/add.js(v=1.0.0.01)}"></script> 14 <script> 15 var url = ctx+‘/wsbsdt/file/uploadFormFile‘; 16 layui.use(‘upload‘, function() { 17 var $ = layui.jquery, upload = layui.upload; 18 var uploadInst = upload.render({ //允许上传的文件后缀 19 elem: ‘#uploadFile‘ 20 ,url: url 21 ,accept: ‘file‘ //普通文件 22 ,exts: ‘zip‘ //只允许上传zip压缩文件 23 ,size: 1024*30 //设置文件最大可允许上传的大小,单位 KB。不支持ie8/9 24 ,done: function(res){ 25 //如果上传失败 26 if(res.code > 0){ 27 return layer.msg(‘上传失败‘); 28 } 29 //上传成功 30 if(validateFormIndexExist(res.file.filepath)){//验证上传文件根目录下是否包含index.html文件 31 return; 32 } 33 //验证存在index.html文件 34 $(‘#fileId‘).val(res.file.id); 35 $(‘#versionAddress‘).val(res.file.filepath); 36 var demoText = $(‘#demoText‘); 37 demoText.html(‘<span>‘+res.file.filename+‘</span> <a href="/web/manager/wsbsdt/file/downLoadFile?fileNo=‘+res.file.id+‘">下载</a>‘); 38 fileUploadSuccess = true; 39 $("#uploadFile").hide(); 40 } 41 ,error: function(){ 42 var demoText = $(‘#demoText‘); 43 demoText.html(‘<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>‘); 44 demoText.find(‘.demo-reload‘).on(‘click‘, function(){ 45 uploadInst.upload(); 46 }); 47 } 48 }); 49 }); 50 51 //写法1:始终返回true,不正常 52 function validateFormIndexExist(path) { 53 $.ajax({ 54 cache : true, 55 type : "POST", 56 url : ctx+"/wsbsdt/itemProjectForm/validateFormIndexExist", 57 data : {path:path},// 你的formid 58 async : false, 59 error : function(request) { 60 parent.layer.alert("Connection error"); 61 }, 62 success : function(data) { 63 if (data.code == 0) { 64 console.log(data) 65 return false; 66 } else { 67 parent.layer.alert(data.msg) 68 } 69 } 70 }); 71 return true; 72 } 73 74 //写法2:始终返回undefined,不正常 75 function validateFormIndexExist(path) { 76 $.ajax({ 77 cache : true, 78 type : "POST", 79 url : ctx+"/wsbsdt/itemProjectForm/validateFormIndexExist", 80 data : {path:path},// 你的formid 81 async : false, 82 error : function(request) { 83 parent.layer.alert("Connection error"); 84 }, 85 success : function(data) { 86 if (data.code == 0) { 87 console.log(data) 88 return false; 89 } else { 90 parent.layer.alert(data.msg) 91 return true; 92 } 93 } 94 }); 95 } 96 97 //写法3:定义一个变量,最后一行返回该变量,正常 98 function validateFormIndexExist(path) { 99 var bool = true; 100 $.ajax({ 101 cache : true, 102 type : "POST", 103 url : ctx+"/wsbsdt/itemProjectForm/validateFormIndexExist", 104 data : {path:path},// 你的formid 105 async : false, 106 error : function(request) { 107 parent.layer.alert("Connection error"); 108 }, 109 success : function(data) { 110 if (data.code == 0) { 111 console.log(data) 112 bool = false; 113 } else { 114 parent.layer.alert(data.msg) 115 } 116 } 117 }); 118 return bool; 119 } 120 </script> 121 </body> 122 </html>
完!
js 写带有返回值的function遇到的返回值不正常的情况
标签:pat doc foo sync ret else oct bsd link
原文地址:https://www.cnblogs.com/zcx-94/p/11957962.html