bootstrap 验证器在我的代码中不工作(没有抛出错误)但在 JSFiddle 上工作

bootstrap validator not working in my code(no errors thrown) but works on JSFiddle

一切正常 JSFiddle

但是,当我尝试在我的 index.html 中使用相同的代码并点击 Request Data 按钮时,它不起作用。以下是代码:

<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.4.5/css/bootstrapvalidator.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.4.5/js/bootstrapvalidator.min.js'></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
        //BEGIN FORM Validations
        $('#validateForm').bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {

                patientSets: {
                    validators: {
                        notEmpty: {
                            message: 'Please select a patient set'
                        }
                    }
                },
                titleOfResearchProject: {
                    validators: {
                        stringLength: {
                            min: 5,
                            message: 'Please Enter the title with minimum 5 letters length'
                        },
                        notEmpty: {
                            message: 'Please Enter title of your project'
                        }
                    }
                },

                descriptionOfResearchProject: {
                    validators: {
                        stringLength: {
                            min: 15,
                            max: 100,
                            message: 'Please enter at least 15 characters and no more than 100'
                        },
                        notEmpty: {
                            message: 'Please Enter Description'
                        }
                    }
                },
                intendedUse: {
                    validators: {
                        notEmpty: {
                            message: 'Please select one option'
                        }
                    }
                },
            }
});

        //END FORM Validations
    </script>
</head>
<body>
<div id="wrapper">

    <div id="page-wrapper">
        <div class="container-fluid">
            <!-- Page Heading -->
            <div class="row" id="main" >

                <!-- BEGIN Bootstrap form testing-->
                <form class="form-horizontal" id="validateForm" method="POST" onsubmit="return false">
                    <div class="row">
                        <div class="col-md-offset-1 col-md-4">

                            <div class="form-group">
                                <label class="control-label">Select your patient sets:</label>
                                <select name="patientSets" class="form-control" >
                                    <option value=" " >Please select patient set</option>
                                    <option>PS1</option>
                                    <option>PS2</option>
                                </select>
                            </div>



                            <div class="form-group">
                                <label class="control-label">Description of research project:</label>
                                <textarea class="form-control" name="descriptionOfResearchProject" placeholder="Enter your description here...."></textarea>
                              </div>


                        </div>
                        <div class="col-md-offset-1 col-md-4">
                            <div class="form-group">
                                <label class="control-label">Title of your research project:</label>
                                <input name="titleOfResearchProject" placeholder="Enter your title here...." class="form-control" type="text">
                              </div>

                              <div class="form-group">
                                <label class="control-label">Intended use:</label>
                                <select name="intendedUse" class="form-control" >
                                    <option value=" " >Please select one</option>
                                    <option>test1</option>
                                    <option>test2</option>
                                </select>
                            </div>



                        </div>
                    </div>

                    <button class="btn btn-success" id="conceptsButton">Request Data</button>



                    </form>
             </div>

    </div>
    <!-- /#page-wrapper -->
</div><!-- /#wrapper -->



</body>
</html>

以下是上述 index.html 页面的屏幕截图,它位于 XAMPP 的 htdocs 的 public 文件夹中。

并且开发者控制台没有显示如下所示的错误;

那是因为您尝试在 validateForm 对象加载之前初始化验证器。将您的脚本移到 </body>

前面

您的代码有一些损坏的链接,并且您没有在文档 'ready' 上初始化您的验证函数,而且您不应该在加载 validateForm 对象之前初始化您的验证器。我在下面修复了这些问题:


    <!DOCTYPE html>
    <html>
    <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <!-- fixed broken link -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.4.5/css/bootstrapvalidator.min.css" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.4.5/js/bootstrapvalidator.min.js'></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> <!-- fixed broken link -->

    </head>
    <body>
    <div id="wrapper">

        <div id="page-wrapper">
            <div class="container-fluid">
                <!-- Page Heading -->
                <div class="row" id="main" >

                    <!-- BEGIN Bootstrap form testing-->
                    <form class="form-horizontal" id="validateForm" method="POST" onsubmit="return false">
                        <div class="row">
                            <div class="col-md-offset-1 col-md-4">

                                <div class="form-group">
                                    <label class="control-label">Select your patient sets:</label>
                                    <select name="patientSets" class="form-control" >
                                        <option value=" " >Please select patient set</option>
                                        <option>PS1</option>
                                        <option>PS2</option>
                                    </select>
                                </div>



                                <div class="form-group">
                                    <label class="control-label">Description of research project:</label>
                                    <textarea class="form-control" name="descriptionOfResearchProject" placeholder="Enter your description here...."></textarea>
                                  </div>


                            </div>
                            <div class="col-md-offset-1 col-md-4">
                                <div class="form-group">
                                    <label class="control-label">Title of your research project:</label>
                                    <input name="titleOfResearchProject" placeholder="Enter your title here...." class="form-control" type="text">
                                  </div>

                                  <div class="form-group">
                                    <label class="control-label">Intended use:</label>
                                    <select name="intendedUse" class="form-control" >
                                        <option value=" " >Please select one</option>
                                        <option>test1</option>
                                        <option>test2</option>
                                    </select>
                                </div>



                            </div>
                        </div>

                        <button class="btn btn-success" id="conceptsButton">Request Data</button>



                        </form>
                 </div>

        </div>
        <!-- /#page-wrapper -->
    </div><!-- /#wrapper -->



    <script type="text/javascript">
            //Initialize function when document 'is ready'
            $(document).ready(function() {
        //BEGIN FORM Validations
            $('#validateForm').bootstrapValidator({
          feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {

            patientSets: {
                validators: {
                    notEmpty: {
                        message: 'Please select a patient set'
                    }
                }
            },
            titleOfResearchProject: {
                validators: {
                    stringLength: {
                        min: 5,
                        message: 'Please Enter the title with minimum 5 letters length'
                    },
                    notEmpty: {
                        message: 'Please Enter title of your project'
                    }
                }
            },

            descriptionOfResearchProject: {
                validators: {
                    stringLength: {
                        min: 15,
                        max: 100,
                        message: 'Please enter at least 15 characters and no more than 100'
                    },
                    notEmpty: {
                        message: 'Please Enter Description'
                    }
                }
            },
            intendedUse: {
                validators: {
                    notEmpty: {
                        message: 'Please select one option'
                    }
                }
            },
        }
    });

            //END FORM Validations
    });
            //END FORM Validations
        </script>
    </body>
    </html>