当我在上传文件时判断是否有文件选中时,加入了如下代码:
<script type="text/javascript">
function getValue()
{
console.log("123");
var x=document.getElementById("chooseFile").value;
if(x=="")
{
alert("请选择文件");
return false;
}
return true;
}
</script>
html代码如下:
<form th:action="@{/uploadFile}" method="post" enctype="multipart/form-data">
<input id="chooseFile" type="file" name="fileUpload" />
<input type="submit" onclick="getValue()" value="上传文件" />
</form>
结果不选择文件依然会提交!
修改了下onclick,就可以了!