<div id="add_yuliao_div" style="width: 50%;background-color: white;border: 1px solid black;
position: absolute;left: 25%;top: 30px;padding-left: 10px;
box-shadow: 4px 4px 8px grey;border-radius: 5px;
display: none">
<br>
<form action="http://127.0.0.1:8080/add_yuliao/" method="post" enctype="multipart/form-data" id="myForm">
<div class="form-group">
<label for="yuliao_name">语料库名称:</label>
<input id="yuliao_name" name="yuliao_name" type="text" placeholder="请输入语料库名字" style="width: 50%">
<br>
<br>
<label for="exampleFormControlFile1">上传语料TXT文件,每条语料一行</label>
<input type="file" name="file" class="form-control-file" id="exampleFormControlFile1">
<br>
<button id="myButton" type="submit" class="btn btn-success">确定上传</button>
</div>
</form>
<br>
<button onclick="javascript:document.getElementById('add_yuliao_div').style.display='none';document.location.reload()" class="btn btn-danger">取消</button>
<br><br>
</div>
<!--判断是否上传文件-->
<script>
document.getElementById('myForm').onsubmit = function(event) {
console.log('here')
const fileInput = document.getElementById('exampleFormControlFile1');
const yuliaoname = document.getElementById('yuliao_name');
if (yuliaoname.value === '') {
alert('请输入语料库名字!');
event.preventDefault(); // 阻止表单提交
}
if (fileInput.value === '') {
alert('请上传文件!');
event.preventDefault(); // 阻止表单提交
}
};
</script>
标签:语料,表单,getElementById,html,提交,document,上传,event
From: https://blog.csdn.net/daxiashangxian/article/details/141322296