使用jQuery的表单验证文件输入不起作用

时间:2016-09-05 22:29:31

标签: javascript jquery spring-boot thymeleaf

我正在尝试完成一些文件的多上传,它要求所有文件都上传到表单中。我正在使用jQuery Validation。这是我的HTML代码:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 
      layout:decorator="layout">
<head>
    <title>Documentos</title>
    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
    <script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/additional-methods.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#myform").validate({
                rules: {  
                    file:{
                        required: true,
                        extension: "pdf"  
                    }  
                },
                messages: {
                     file:{
                       required: "Please select the document!",
                       accept: "You can only upload PDF files."
                    } ,
                }       
            });
        });
    </script>
</head>
<body>
    <div layout:fragment="header">
        <h1>
            <span>Empleado</span>
            <i><b th:text="${employee.name}"></b></i>
        </h1>
    </div>
    <div layout:fragment="content">
           <div class="box box-default">
                <div class="box-header with-border">
                    <h3 class="box-title">
                        <span>Perfil</span>
                        <i><b th:text="${employee.profile.name}"></b></i>
                    </h3>
                </div>
                <form id="myform" th:action="@{'/employee/' + ${employee.id} + '/save/documents'}" enctype="multipart/form-data" method="POST" class="form-horizontal">
                    <div class="box-body">
                        <table id="files-list" class="table table-bordered table-striped">

                            <tr th:each="documento : ${documentsList}">
                                <td th:text="${document}"/>
                                <td><input type="file" name="file" id="file"/></td>
                            </tr>
                        </table>

                    </div>
                    <div class="box-footer">
                        <button type="submit" class="btn btn-primary">Upload Documents</button>
                    </div>
                </form>
           </div>
    </div>
</body>
</html>

每次接受输入中的空字段。我究竟做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分:0)

看起来它对我来说很好......

JSFiddle DEMO

您的<head>内容混合了 - 这可能就是问题所在。所有CDN都应该是http或https:

<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/additional-methods.min.js"></script>