html表单提交前判断

 <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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值